2

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"

Gouz
  • 336
  • 1
  • 6
  • 19

1 Answers1

1

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

enter image description here

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
  • I did and I have granted them. Then, after checking from the App properties, all are allowed and nothing Denied. – Gouz Mar 05 '20 at 02:45
  • weird cause this "/storage/emulated/0" seems too short to be your apps directory. maybe its really up to the device I wouldnt know – quealegriamasalegre Mar 05 '20 at 02:51
  • The "/storage/emulated/0" is the root of the ExternalStorageDirectory (which is the parent). My App is trying after pressing a button to create a dir ("MyApp") within the above destination (i.e. at "/storage/emulated/0/MyApp") but it fails because it cannot write I guess (or read) within the root. – Gouz Mar 05 '20 at 02:56
  • have you checked this? https://stackoverflow.com/questions/39462667/titanium-android-external-storage-create-new-directory-and-then-write-files/39462762#39462762 – quealegriamasalegre Mar 05 '20 at 02:56
  • thats strange, I never had to "press a button". normally externalstorade is created automatically for my apps. I guess its normal for your app not to be able to read the root as else your app would be able to wreak havoc on your other apps data – quealegriamasalegre Mar 05 '20 at 02:59
  • Hey I just found out you are using a deprecated method. see this https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory() it got deprecated in android O so maybe thats the reason now there seems to be Context.getExternalFilesDir(null) whic should return your actual apps storage "/storage/emulated/0/com/example/android/yourApp" – quealegriamasalegre Mar 05 '20 at 03:28
  • getExternalStorageDirectory() is not available since Android Q. What is this Mate 20? – blackapps Mar 05 '20 at 07:30
  • no, as I read it I think it just doesnt return that directory when your app targets Q – quealegriamasalegre Mar 05 '20 at 08:08
  • How to media scan the files/images inserted using getExternalFilesDir, so that it will be available in the gallery. I've tried by using MediaScannerConnection.scanFile. but it didn't work. – Parag Pawar Sep 26 '20 at 12:38