Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

Android content Uri cannot be played by other app

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...