I download file with 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.
}
Download completes (it can be opened from device file explorer in AS) and then i try to share it (e.g. i have file named "Export File.pdf", exists() returns 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)
Debug values:
file = /storage/emulated/0/Download/Export File.pdf
fileUri=content://com.thirdframestudios.android.expensoor.cache_directory_provider/Download/Export%20File.pdf
Share sheet is shown (tested on emulator api 29), but when i select some app (e.g. g. drive, i get error when it tries to upload file). Similiar thing happens if i try to open file with ACTION_VIEW (pdf viewer is opened for a brief moment and then closes). I get error when trying to open file with 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
My file provider:
<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>
My file_paths.xml file:
<!-- 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/" />