簡體   English   中英

無法與其他應用程序共享或打開下載到“下載”文件夾的 pdf 文件

[英]Can not share or open pdf file downloaded to "Download" folder with other apps

我用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 上測試),但是當我選擇某個應用程序時(egg drive,當它嘗試上傳文件時出現錯誤)。 如果我嘗試使用 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM