AndroidQ DownloadManager.Request.setDestinationUri [英] AndroidQ DownloadManager.Request.setDestinationUri

查看:101
本文介绍了AndroidQ DownloadManager.Request.setDestinationUri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的下载代码存储必须生存"才能卸载应用程序的图像,我使用了 getExternalStoragePublicDirectory ,但是现在在Android Q上已弃用该API,因此我修改了我的下载代码

My download code stores images that must 'survive' to app uninstall, I used getExternalStoragePublicDirectory, but now on Android Q this API is deprecated so I modified my download code

val uri = Uri.fromFile(File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "mysubdir"))
val request = DownloadManager.Request(Uri.parse(url))
.setDestinationUri(uri)

val uri = buildImageUri(context.contentResolver, fileName, fileName)
val request = DownloadManager.Request(Uri.parse(url))
    .setDestinationUri(uri)

////// Use MediaStore

    fun buildImageUri(
        cr: ContentResolver,
        title: String,
        description: String): Uri? {
        val values = ContentValues()
        values.put(MediaStore.Images.Media.TITLE, title)
        values.put(MediaStore.Images.Media.DISPLAY_NAME, title)
        values.put(MediaStore.Images.Media.DESCRIPTION, description)
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
        val buildTime = System.currentTimeMillis() / 1000
        values.put(MediaStore.Images.Media.DATE_ADDED, buildTime)
        values.put(MediaStore.Images.Media.DATE_MODIFIED, buildTime)

        return cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
    }

但是我得到了 java.lang.IllegalArgumentException:不是文件URI:null ,因此必须如何编写代码以使其与Android Q兼容,并且不使用诸如uri和分割字符串中的搜索冒号之类的深奥技巧?

But I obtain java.lang.IllegalArgumentException: Not a file URI: null so how the code must be written to be Android Q compliant and without using esoteric tricks like search colon in uri and split string?

setDestinationUri 未更新,并说

对于定位到Build.VERSION_CODES.Q或更高版本的应用, omissimis文本或顶级Downloads目录中的路径(如由Environment#getExternalStoragePublicDirectory(String)返回环境#DIRECTORY_DOWNLOADS)

For applications targeting Build.VERSION_CODES.Q or above, omissimis text or a path within the top-level Downloads directory (as returned by Environment#getExternalStoragePublicDirectory(String) with Environment#DIRECTORY_DOWNLOADS)

推荐答案

DownloadManager 仍然有其长期的局限性,即仅支持 file:方案,而不支持内容:.我提出了一个问题,试图有朝一日纠正这个问题.

DownloadManager still has its long-standing limitation of only supporting file: schemes, not content:. I filed an issue to try to get this corrected someday.

您的选择是:

  • DownloadManager.Request 上使用 setDestinationInExternalFilesDir(),该映射到 getExternalFilesDir(),您的应用具有完全访问权限.如果内容是应在应用程序卸载后幸存的内容,则需要将其用作临时位置,然后将内容自己移动到另一个更持久的位置.

  • Use setDestinationInExternalFilesDir() on DownloadManager.Request, which maps to getExternalFilesDir(), which your app has full access to. If the content is something that should survive an app uninstall, you would need to use this as a temporary location and move the content to another, more durable location yourself.

DownloadManager.Request 上使用 setDestinationInExternalPublicDir().尽管不建议使用相应的位置( Environment.getExternalStoragePublicDirectory()),但这并不妨碍您对其使用 DownloadManager .可能的问题是,一旦您的应用程序采用了Android Q的范围存储,您将无法读取或写入下载的文件,因为您无权访问此目录.因此,这仅适用于盲目"下载,在这种情况下,您的应用程序应用户要求下载内容,但只有用户(而非应用程序)需要访问下载的内容.

Use setDestinationInExternalPublicDir() on DownloadManager.Request. While the corresponding location (Environment.getExternalStoragePublicDirectory()) is deprecated, that does not prevent you from using DownloadManager for it. What may be a problem is that you will not be able to read or write the downloaded file, as you have no access to this directory, once your app is adopting Android Q's scoped storage. So, this is suitable only for "blind" downloads, where your app is downloading something at user request but only the user (not the app) needs access to the downloaded content.

DownloadManager.Request 上使用 setDestinationUri()来获取一些文件: Uri 其他有用的位置.例如,可能(有点奇怪)想要下载到 getExternalCacheDir().

Use setDestinationUri() on DownloadManager.Request for some file: Uri that represents some other useful location. For example, it is possible (though a bit odd) to want to download to getExternalCacheDir().

这篇关于AndroidQ DownloadManager.Request.setDestinationUri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆