I've ported my application to android API 24. My application downloads file to a folder called test
in external storage, and after download it will open the download file.
As an example consider file with address /storage/emulated/0/test/video.mp4
. Its address is created usign this syntax:
File file = new File(Environment.getExternalStorageDirectory()+ "/test/" + fileName);
To create its URI to load it using an intent, I used instructions in this answer. Here is the provider tag in my manifest (which is nested inside application
tag):
<provider
android:name="android.support.v4.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>
And here is the provide in /res/xml/provider_paths.xml
:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="test" />
</paths>
After all, when I use this syntax to create the URI:
uri = FileProvider.getUriForFile(mCurrentActivity, mCurrentActivity.getPackageName() + ".provider", file);
and it will result uri string content://my.package.name.provider/external_files/video.mp4
.
However trying to open URI using intents, file is not opened in application related to that mime-type (I've checked the file and it is downloaded and can be played/opened in viewers with no problem).
Here is the code to open the URI which if fully functional in versions less than 24 using Uri.FromFile
:
intent.setDataAndType(uri, mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(
mCurrentActivity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Based on comments I updated the code to this but it does not change the situation:
intent.setDataAndType(uri, mimeType)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PendingIntent contentIntent = PendingIntent.getActivity(
mCurrentActivity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);