Ed Kuhner
Ed Kuhner

Reputation: 428

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

Upvotes: 7

Views: 37415

Answers (6)

Gopal Reddy
Gopal Reddy

Reputation: 36

For Android 11 and above Use this before requesting

Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                        Uri uri = Uri.fromParts("package", getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);

To retrview and Store :

        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/FolderName/"+new_filename+"/");
        File[] allfiles = null;
        allfiles = file.listFiles();

        Log.i(TAG, String.valueOf(allfiles.length));

Upvotes: 0

I_4m_Z3r0
I_4m_Z3r0

Reputation: 1080

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 (:

Upvotes: 2

Ed Kuhner
Ed Kuhner

Reputation: 428

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.

Upvotes: 10

Dharmik Mavani
Dharmik Mavani

Reputation: 107

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

android:requestLegacyExternalStorage="true"

Upvotes: 1

axar
axar

Reputation: 539

please describe your question properly

if you want to pickup file from external storage then also apply in manifest requestlegegyexternalstorage=true

see in these snap - http://prntscr.com/114kqxp

or if you want to store the file in the device then you have to use file provider in android

for file provider example

please visit link - couldn't attach file send email android 11

Upvotes: -1

IChung
IChung

Reputation: 459

Do you have the following permission in your manifest? You say you have write and manage permission, but I think you actually require read permission to read external storage.

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

Upvotes: 0

Related Questions