问题描述
我使用downloadManager下载文件
private fun downloadFile(exportModel: Export,i: Int) {
val request: DownloadManager.Request = DownloadManager.Request(Uri.parse(exportModel.data.path + exportModel.data.files[i].filename))
.setTitle(exportModel.data.files[i].filename)
.setDescription("Downloading")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setAllowedOverMetered(true)
.setAllowedOverRoaming(true)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,exportModel.data.files[i].filename)
val downloadManager = activity?.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
val downloadID = downloadManager.enqueue(request) // enqueue puts the download request in the queue.
}
下载完成(可以从AS中的设备文件资源管理器中打开),然后尝试共享它(例如,我有一个名为“ Export File.pdf”的文件,exists()返回true):
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"Export File.pdf")
val fileUri = getUriForFile(requireContext(),"com.thirdframestudios.android.expensoor.cache_directory_provider",file);
val shareIntent: Intent = Intent().apply {
setDataAndType(fileUri,context!!.contentResolver.getType(fileUri))
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM,fileUri)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
startActivity(shareIntent)
调试值:
file = /storage/emulated/0/Download/Export File.pdf
fileUri=content://com.thirdframestudios.android.expensoor.cache_directory_provider/Download/Export%20File.pdf
显示了共享表(已在仿真器api 29上测试),但是当我选择某些应用程序(例如驱动器时,尝试上载文件时出现错误)。如果我尝试使用ACTION_VIEW打开文件,则会发生类似的事情(打开pdf查看器一会儿然后关闭)。 尝试使用ACTION_VIEW打开文件时出现错误:
2020-10-21 12:58:32.183 18445-18496/? E/DisplayData: openFd: java.io.FileNotFoundException: open failed: EACCES (Permission denied)
2020-10-21 12:58:32.183 18445-18496/? E/PdfLoader: Can't load file (doesn't open) Display Data [PDF : Export File.pdf] +ContentOpenable,uri: content://com.thirdframestudios.android.expensoor.cache_directory_provider/Download/Export%20File.pdf
我的文件提供商:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.thirdframestudios.android.expensoor.cache_directory_provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
我的file_paths.xml文件:
<!-- used for support cache -->
<cache-path
name="support_cache"
path="."/>
<!-- Based on default Glide operation,see InternalCacheDiskCacheFactory instantiated from GlideBuilder.createGlide() -->
<cache-path
name="share"
path="image_manager_disk_cache"
tools:path="DiskCache.Factory.DEFAULT_DISK_CACHE_DIR" />
<!-- used for sharing export files -->
<external-path
name="Download"
path="Download/" />
解决方法
注册一个广播接收器,以在下载管理器准备就绪时通知您。
您可以从下载管理器中获取下载文件的uri,然后在ACTION_VIEW中使用。
或使用setDestinationInExternalFilesDir()下载到getExternalFilesDir()。
,搜索了几个小时后,我找到了这样的解决方案。只需删除发出下载请求的目录设置即可。线下。在 Android 11 上测试。
setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,exportModel.data.files[i].filename)
// 删除上面的行。最后下载请求看起来像:>
val request = DownloadManager.Request(Uri.parse(url))
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE or DownloadManager.Request.NETWORK_WIFI)
request.setTitle(requireContext().getString(R.string.downloaded))
request.setDescription(keyValue)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
manager.enqueue(request)