Reputation: 929
Not sure why I'm getting this as I thought I followed the example here correctly.
Many others have asked questions around this same thing but after looking through the suggestions it's still broken.
The full exception looks like this:
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.company.app/files/app_videos/VIDEO_20210202_184514.mp4
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
The manifest looks like this:
<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>
The provider_paths.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="cache" path="/"/>
<external-cache-path name="external_cache" path="." />
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<files-path name="app_videos" path="files/" />
</paths>
The code I am using (pretty much cut and paste from Google's site) looks like this:
val imagePath = File(context.filesDir, "app_videos")
Timber.i("imagePath: ${imagePath.name}")
val newFile = File(imagePath, name)
Timber.i("newFile exists: ${newFile.exists()} length: ${newFile.length()}")
val uri = FileProvider.getUriForFile(context.applicationContext, context.packageName + ".provider", newFile)
Timber.i("Adding Uri: $uri")
That last line never executes as the crash happens on the line before it.
Now, I know that I saved the file off here: /data/user/0/com.company.app/files/VIDEO_20210202_/data/user/0/com.standardandroid.stalkersport/files/VIDEO_20210202_200455.mp4
I know it's there because I can play it back just fine using other code
So, I know the file exists. I just can't create a Uri for it.
Can anyone see what I am doing wrong please?
Upvotes: 1
Views: 382
Reputation: 929
OK...got it. Needed to tweak a couple things. First the code should look like this
private fun shareMultipleVideos(names: List<String>, context: Context) {
val uris: ArrayList<Uri> = ArrayList()
for (name in names) {
val videoFile = File(context.filesDir, name)
Timber.i("videoFile ${videoFile.name} exists: ${videoFile.exists()}")
val uri = FileProvider.getUriForFile(context.applicationContext, context.packageName + ".provider", videoFile)
Timber.i("Adding Uri: $uri")
uris.add(uri)
}
val intent = Intent()
intent.action = Intent.ACTION_SEND_MULTIPLE
intent.putExtra(Intent.EXTRA_SUBJECT, "Shared files")
intent.type = "video/mp4"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Timber.i("Intent: $intent")
try{
ContextCompat.startActivity(context, Intent.createChooser(intent, "Shared Videos"), null)
} catch (e: Exception) {
Timber.e("Exception starting activity. \nException was ${e.message}\n Stack trace to follow:\n ${e.stackTrace}")
}
}
Next, the paths xml file needed to be tweaked a bit. Here are the results there:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="cache" path="/"/>
<external-cache-path name="external_cache" path="." />
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<files-path name="app_videos" path="." />
</paths>
Upvotes: 1