简体   繁体   English

是否可以以编程方式访问 Android Q(SDK >= 29)上的下载文件夹?

[英]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.我想列出下载文件夹中的所有文件.txt ,然后允许用户选择一个并阅读其内容。 My minSdkVersion is 16 , but I came across this problem because my Android is Q (29).我的minSdkVersion16 ,但我遇到了这个问题,因为我的 Android 是 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.显然Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)可以为 SDK <= 29 解决它。但是,我没有测试它,因为我想首先查看 Android Q 的解决方案。

getExternalStoragePublicDirectory says: getExternalStoragePublicDirectory说:

When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps.当应用以 Build.VERSION_CODES.Q 为目标时,应用无法再直接访问从此方法返回的路径。 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 .通过迁移到Context#getExternalFilesDir(String)MediaStoreIntent#ACTION_OPEN_DOCUMENT等替代方案,应用程序可以继续访问存储在共享/外部存储上的内容。

So:所以:

  • getExternalFilesDir returns /storage/emulated/0/Android/data/com.mypackage/files/Download . getExternalFilesDir返回/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.此路径没有用,我希望它返回/storage/emulated/0/Download ,这是下载文件所在的位置。

  • I didn't manage to do something with MediaStore.Downloads .我没有设法对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.".我检查了文档,它说“特别是,如果您的应用程序想要访问您的应用程序未创建的 MediaStore.Downloads 集合中的文件,则必须使用存储访问框架。”。 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.在我的概念中, IntentStorage Access Framework需要用户使用文件管理器浏览目录和文件,这不是我在这里想要的。

In short:简而言之:

Is it possible to list the .txt downloaded files programmatically in Android Q (SDK >= 29)?是否可以在 Android Q (SDK >= 29) 中以编程方式列出.txt下载的文件? If so, how?如果是这样,如何?

Is it possible to list the .txt downloaded files programmatically in Android Q (SDK >= 29)?是否可以在 Android Q (SDK >= 29) 中以编程方式列出 .txt 下载的文件?

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).最接近的是,如果您使用ACTION_OPEN_DOCUMENT_TREE并要求用户打开外部存储上的Downloads/树...而您不能在 Android R 上这样做(至少通过 DP1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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