Is it possible to programmatically access Download folder on Android Q (SDK >= 29)?

Rafael Tavares :

What I want:

I want to list all files .txt in the Download folder and then allow the user to pick one and read its content. My minSdkVersion is 16, but I came across this problem because my Android is Q (29).

What I tried:

I've been researching and apparently Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) would solve it for SDK <= 29. However, I didn't test it because I would first like to check out a solution for Android Q.

getExternalStoragePublicDirectory says:

When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

So:

  • getExternalFilesDir returns /storage/emulated/0/Android/data/com.mypackage/files/Download. This path is not useful, I expected it to return /storage/emulated/0/Download, which is where the downloaded files are.

  • I didn't manage to do something with MediaStore.Downloads. I checked the documentation and it says "In particular, if your app wants to access a file within the MediaStore.Downloads collection that your app didn't create, you must use the Storage Access Framework.". So, I assume it won't work for me.

  • In my concept, Intent and Storage Access Framework would need the user to navigate through directories and files using a File Manager, which is not what I want here.

Maybe relevant

There is a share feature in my app, so I am using a FileProvider. I don't know if it is conflicting with the getExternalFilesDir. I tried removing FileProvider but there were no changes in the getExternalFilesDir behavior.

AndroidManifest.xml

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="share" path="."/>
</paths>

In short

Is it possible to list the .txt downloaded files programmatically in Android Q (SDK >= 29)? I wanted to do it programmatically because lay users may not find the necessary files if they need to use a file manager (which is my current solution).

CommonsWare :

Is it possible to list the .txt downloaded files programmatically in Android Q (SDK >= 29)?

Not ones created by other apps, which would appear to be your intention.

The closest is if you use ACTION_OPEN_DOCUMENT_TREE and ask the user to open the Downloads/ tree on external storage... and you cannot do that on Android R (at least through DP1).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8438&siteId=1