Android content Uri cannot be played by other app

ⅰ亾dé卋堺 提交于 2021-01-29 09:15:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!