Reputation: 377
Running my App on a Huawei Mate 20, the following lines:
String ext_path = Environment.getExternalStorageDirectory().getAbsolutePath();
File ext_folder = new File(ext_path);
System.out.println("Path: " + ext_path + " Dir Check: " + ext_folder.isDirectory() + " Can read: " + ext_folder.canRead());
Return: "Path: /storage/emulated/0 Dir Check: true Can read: false"
However, I have given all permissions and I have also included these in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Shouldn't canRead() return True? I cannot write or read files. What could be the reason?
I also tried:
String extStore = System.getenv("EXTERNAL_STORAGE");
File f_exts = new File(extStore);
System.out.println(f_exts);
System.out.println(Arrays.toString(f_exts.listFiles()));
and I get: "EXTERNAL_STORAGE" and "null"
Upvotes: 2
Views: 1764
Reputation: 3258
you need to explicitly ask the user to grant said permissions. else it wont work in newer android vesions.try adding this in onCreate
ActivityCompat.requestPermissions(MainActivity.this, new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE},111);
And also
If you are TARGETING Android Q also Environment.getExternalStorageDirectory() no longer leading to an accessible folder, which i think is actually your problem. it is deprecated in favor of Context.getExternalFilesDir(null) which returns a file. that is worth a try as your apps external storage should be something like "/storage/emulated/0/Android/data/com.example.android.YourApp/" and on that one you should have read and write access
Upvotes: 1