9

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?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
  • In Android Q you can. https://stackoverflow.com/questions/60413633/cant-read-data-from-external-storage-android-emulator – blackapps Feb 26 '20 at 15:16
  • `There is a share feature in my app, so I am using a FileProvider. ` That has nothing to do with your wanted access. – blackapps Feb 26 '20 at 15:19

1 Answers1

1

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I just found [this article](https://commonsware.com/blog/2019/06/07/death-external-storage-end-saga.html) on your blog. It's pretty elucidating about the storage subject. – Rafael Tavares Feb 26 '20 at 16:17
  • What about getting the self-created files by the apps after reinstall app? https://stackoverflow.com/questions/63593112/do-we-need-permission-to-get-all-files-that-is-self-created-by-the-app-after-the – jazzbpn Aug 27 '20 at 08:54
  • is there no other way to do this nowadays? no other way than asking the use to manually open the `Downloads/` folder? – noloman Aug 16 '22 at 12:24
  • @noloman: You do not have independent access to files created by other apps in `Downloads/`, if that is what you are asking. – CommonsWare Aug 16 '22 at 12:28
  • yes, indeed that's what I was asking: I thought the `Download` folder was an external directory where files could be stored and open, and I thought I could create an app that listed all the filese in the `Download` folder – noloman Aug 16 '22 at 13:06
  • 1
    @noloman: You are welcome to request `MANAGE_EXTERNAL_STORAGE` permission, but unless you have a very good reason for it, your app may be rejected from the Play Store or other app distribution channels. – CommonsWare Aug 16 '22 at 13:41