1

I know there's a lot of questions similar with my issues but none of the answers able fixed my problem.

Please know that on Android 6 below (<6) everything works as expected I can create file into the USB directory. With the same set of "codes" it fails in Android 6. The problem is that the File.canWrite() returns "false" in Android 6 for USB directory.

Here how I list down the "valid" folders/directories:

File rootDir = new File(Environment.getRootDirectory().toString());
List<File> folders = new ArrayList<>();
File[] files = rootDir.listFiles();
for (File file : files) {
    if (isValidDirectory(file) || file.equals(mInitialRoot)) {
        folders.add(file);
    }
}

And this is the isValidDirectory() method do:

private boolean isValidDirectory(File file) {
    return file.isDirectory()
            && file.canWrite()
            && file.canRead()
            && file.exists();
}

I don't know why but the file.canWrite() returns "false" on android 6 thus make the isValidDirectory() to returns "false"

I already added the android.permission.WRITE_EXTERNAL_STORAGE and most of the answers I found are pointing on this permission and since I already declared it I'm not sure where the problem is...

Any help is appreciated thanks in advance.

SquareBox
  • 823
  • 1
  • 10
  • 22
  • `in Android 6 for USB directory.` What is the exact path of that directory? – greenapps Jul 19 '16 at 08:21
  • Which path is given by `Environment.getRootDirectory()` ? /system ? What sense does it make to list the directories in it? – greenapps Jul 19 '16 at 08:34
  • @greenapps actually that's just a test and gonna change that later on. Actually this codes resides in what I called "directory picker" which you can navigate in each directory. So even if I'm in /system I can still navigate to other directory by selecting each folder/directory or go back by selecting the "back". My problem is, when I use the canWrite() the "USB directory" won't appear on the list since it's not writable... but when I remove that the "usb directory" show up in the list but the codes for creating files won't work since its not writable – SquareBox Jul 19 '16 at 23:24
  • What is the exact path of that usb directory i asked. – greenapps Jul 19 '16 at 23:25
  • @greenapps the path for the USB directory is in "/storage". On my android 5 the USB directory is named as "USBDriveA" so the exact path would be "/storage/USBDriveA". In android 6 it has different name "AC79-BB89" so the exact path is "/storage/AC79-BB89". Both device are Samsung tablets. Please note that everything is working in Android 5 only in Android 6 which the directory "AC79-BB89" is not writable. – SquareBox Jul 19 '16 at 23:49
  • But your app can write to its app specific directory on that medium. The path would be `/storage/AC79-BB89/Android/data/ – greenapps Jul 20 '16 at 06:43

3 Answers3

1

It seems I found a way for this. I migrate my codes to use Storage Access Framework and so far it's working. Thanks for the help!

SquareBox
  • 823
  • 1
  • 10
  • 22
0

In Android version >= 6.0 you need to ask user for that permission by using, for example this code:

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
      return true;
} else {
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
      return false;
}
  • The app already asked the user for WRITE_EXTERNAL_STORAGE permission in main_activity... And I can verify that the app has permission "storage" enabled using the "App Info" screen. – SquareBox Jul 19 '16 at 08:17
  • @SquareBox then probably you use File.canWrite not in external storage. Android has external and internal storage. You can use only external storage, read this article [Find an external SD card location](http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location) – Stanislav Parkhomenko Jul 19 '16 at 08:25
0

You have to get run time permission in Android 6.0

Look here how to get Runtime permission in my Earlier answer

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41