问题
I used download manager to download a file to external storage. I set the destination to Download folder using
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
After success I retrieve the uri with
val fileUri=cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
which gives me this
file:///storage/emulated/0/Download/dishapatani_Jan%2007%2C%2008%3A07_1609986166669.mp4
Now I try to play this video file using intent with this
val uri=Uri.parse(" file:///storage/emulated/0/Download/dishapatani_Jan%2007%2C%2008%3A07_1609986166669.mp4")
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val contentUri = FileProvider.getUriForFile(requireContext(), requireContext().packageName + ".provider", File(localUri))
intent.setDataAndType(contentUri, "video/mp4")
intent.setData(contentUri)
startActivity(intent)
Here content Uri is content://com.myapp.provider/external_path/dishapatani_Jan%252007%252C%252008%253A07_1609986166669.mp4
But it neither does played not it is recognized by other app when I use action_share intent.
Also the file uri that has file:// has to be removed and replaced with /storage/emulated/0/Download/dishapatani_Jan%2007%2C%2008%3A07_1609986166669.mp4" else app crashes saying no root configured.
Here is my provider path
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_path" path="/Download" />
</paths>
and manifeast
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
回答1:
Dont mess around with paths and uries and your own file provider.
You got -or can get- an uri from the downloadmanager.
Use that to play the file.
来源:https://stackoverflow.com/questions/65606320/android-content-uri-cannot-be-played-by-other-app