Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!!

like image 566
Ed Kuhner Avatar asked Jan 24 '23 10:01

Ed Kuhner


1 Answers

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.

like image 95
Ed Kuhner Avatar answered Jan 31 '23 17:01

Ed Kuhner