Android 11 - Accessing Files in my app Android/Data folder

16,584

Solution 1

I Think I Have An Answer?

The more I read about this - it appears that Android 10 went one way, and then Android 11 back tracks it a little and re-enables some of the direct file path access.

Hopefully I'm right on this and won't have to come back and re-do things again down the road

So the answer is to use the requestLegacyExternalStorage in the Manifest - even though it's got all kinds of warnings

And then I created a folder in Documents with this

File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+File.separator+"MyAppFolder");
filePath.mkdirs();

And Now I can write to this and read from it.

Anyway - this seems to work for me on Android 9, 10 and 11. So hopefully the trend forward continues.

Solution 2

Google Play Store requires that you use SCOPED STORAGE and not SHARED STORAGE, so you should avoid to use "requestLegacyStorage".

This mean you cannot access files on the sd card if the user doesn't directly choose them. To select a file from external storage you need the user interaction (:

There's another solution with Android >= 10 and it is the permission "MANAGE_EXTERNAL_STORAGE" which let you choose an application which can access the external storage. But the user must do all the actions so it isn't a really good solution :/

If your problem is about files you are taking with user interaction that are in the external storage, you can enable the access to the file uri by using a fileProvider (from example when you take a photo).

Have a nice day and a nice coding (:

Solution 3

Add below line in your Android manifest file in application tag.

android:requestLegacyExternalStorage="true"
Share:
16,584
Ed Kuhner
Author by

Ed Kuhner

Updated on June 15, 2022

Comments

  • Ed Kuhner
    Ed Kuhner about 2 years

    I'm really struggling with this for some reason and am hoping someone can help point me in the right direction.

    I am targeting Android 11 / API 30 which is where the trouble seems to all stem from. Targeting lower might work for me - but it seems like Google is going to force me down this path eventually so I might as well just figure this out.

    My apps typically write files out to the standard

    getExternalFilesDir(null)
    

    This gives me a path on the device which is

    /storage/emulated/0/Android/data/com.domain.testapp/files
    

    I also tried other types, like:

    getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    

    results in

    /storage/emulated/0/Android/data/com.domain.testapp/files/Documents
    

    My app has no trouble actually writing files out to this location. In Android 11 I have to jump through some hoops with the local file explorer to see these files - but they do exist on the device.

    I am now trying to give the user some ability to see the files and share the files and that's where I'm stumped!

    This code below results in no files found

    File filePath = this.getExternalFilesDir(null);
    String filePathString = filePath.toString();
    
    ArrayList<String> myData = new ArrayList<>();
    File fileDir = new File(filePathString);
    
    String[] files = fileDir.list();
    
    if(files.length == 0){
        Log.w(APPID, "NO FILES IN FOLDER");
    }
    

    Yes - I know the File and .toString() is not needed - but I was logging them out each step because I thought I was crazy.

    I know for a FACT that there are a dozen or so files in the folder this is pointing to in this app. This app created the files. Shouldn't it be able to see the files in the folder???

    Manifest has the following permissions - which I dont think it needs all of:

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

    No permissions show as denied to the app

    I thought about using the external storage - but it seems like this is even harder in Android 11? Or is that really an easier path to take?

    I did try playing with this, but it seems deprecated and soon to be gone?

    File downloadFilePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    

    which is this folder on my device

    /storage/emulated/0/Download
    

    And I can definitely see the files in there with any file explorer.

    Any thoughts on how to get access to the files this app created?

    I'm probably missing something really obvious.

    Any suggestions welcomed!!

  • Ed Kuhner
    Ed Kuhner about 3 years
    This is incorrect for Android 11 - according to Google: Caution: After you update your app to target Android 11 (API level 30), the system ignores the requestLegacyExternalStorage attribute when your app is running on Android 11 devices, so your app must be ready to support scoped storage and to migrate app data for users on those devices. developer.android.com/training/data-storage/…
  • Ed Kuhner
    Ed Kuhner about 3 years
    Turns out you are correct - but that wasn't the entire solution for me because I still could not read the contents of this folder properly.
  • Sakhawat Hossain
    Sakhawat Hossain about 3 years
    This doesn't solve the problem now-a-days.
  • Ed Kuhner
    Ed Kuhner about 3 years
    It does. android studio will tell you it's deprecated - but it works. you need this in the manifest as well: android:requestLegacyExternalStorage="true"
  • Umeshkumar D.
    Umeshkumar D. over 2 years
    This way you just bypass current requirement of Scoped storage. But you did not follow scoped storage best practice for that.
  • Pakz
    Pakz over 2 years
    What is your targetSdkVersion inorder to access storage using Android 11?