简体   繁体   中英

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

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:

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.

In short:

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM