0

Currently I'm storing my file(images/videos) like this:

File directoryToStore;
directoryToStore = getBaseContext().getExternalFilesDir("MyImages");

This will return this path:

/storage/emulated/0/Android/data/pacageName/files/MyImages/

Now, I want to store the files in root directory /storage/emulated/0/MyImages/. I have tried this by doing:

File directoryToStore;
directoryToStore = new File(Environment.getExternalStorageDirectory(), "MyImages");

This works perfectly fine when running on pre-Marshmallow devices, but In Marshmallow the files are not found.


My Question

How should/can I store files in the root directory so that the file will be found in all API's?

ClassA
  • 2,480
  • 1
  • 26
  • 57
  • This could be related with the new permission system, read more at https://developer.android.com/training/permissions/requesting.html – ישו אוהב אותך Aug 11 '17 at 07:18
  • @ישואוהבאותך No it is not related to permissions, I request permissions before doing the action, I have printed a log to check if permissions have been granted. – ClassA Aug 11 '17 at 07:20
  • Then you'll need to [edit] your question to provide a [mcve] that demonstrates the issue, including how you're requesting the relevant permission. – Mike M. Aug 11 '17 at 07:23

1 Answers1

0

Firstly, make sure you have the permissions bellow in your Android Manifest file, because you are trying to save files into the device's external storage.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Secondly, check if storage permissions are granted to your application (when you execute on Marshmallow or higher OS version devices).

Finally, make sure if there is directory named "MyImages" and check it's chmod. If directory does not exist, create it (mkdir() will create the directory in your example) and then try to save your files again.