Sanjana Nair Sanjana Nair - 3 months ago 19
Android Question

Storing data inside Android/data/pkgname

I am trying to add data into a folder which is inside Android/Data/packagename. So I am trying this out:

String tempsubdirtest = Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName() + "/files/Images";


File subdirecttest = new File(tempsubdirtest);

if (!subdirecttest.exists())
{
subdirecttest.mkdirs();
}


The question is that is there an easy way to reduce the code for:

Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName()


instead of me typing /Android/data/ +getPackName etc.

Thanks!

Answer

You can use this existing method getExternalCacheDir

This will return the directory path plus a cache folder. So in your case, you can just exclude that cache path.

/storage/emulated/0/Android/data/com.example.test/cache

You might gonna check this, which will return something similar to what you want.

String pathImage = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();

/storage/emulated/0/Android/data/com.example.test/files/Pictures