问题描述
我已经查看了几篇关于此的帖子,并且我尝试了六个建议的解决方案。我有一些与此几乎相同的工作,而不会在我编写的另一个应用程序中引发异常,但我无法让它不引发异常,尽管文件确实被传输了!正在设置权限并且文件确实传输得很好,但尽管不会使应用程序崩溃,但仍会抛出异常。
我拥有的是这个:
fun shareVideo(videoFile: File,context: Context) {
if(videoFile.exists()) {
Timber.i("Video file exists and the length in bytes is: ${videoFile.length()}")
} else {
Timber.w("Video file does not exist. Exiting.")
return
}
val uris = arraylistof<Uri>()
val uri = FileProvider.getUriForFile(context.applicationContext,context.packageName + ".provider",videoFile)
Timber.i("Uri: $uri + path: ${uri.path}")
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}")
}
}
堆栈跟踪如下所示:
2021-03-07 14:21:59.570 6039-6207/com.company.app E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.company.app.provider/external_files/1615148514218.mp4 from pid=32136,uid=1000 requires the provider be exported,or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:820)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:684)
at android.content.ContentProvider$Transport.query(ContentProvider.java:239)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:106)
at android.os.Binder.execTransactInternal(Binder.java:1154)
at android.os.Binder.execTransact(Binder.java:1123)
Timber 日志输出如下所示:
2021-03-07 14:33:32.713 7396-7396/com.company.app I/StorageUtilsKt: Video file exists and the length in bytes is: 7511455
2021-03-07 14:33:32.719 7396-7396/com.company.app I/StorageUtilsKt: Uri: content://com.company.app.provider/external_files/1615149206171.mp4 + path: /external_files/1615149206171.mp4
2021-03-07 14:33:37.337 7396-7396/com.company.app I/StorageUtilsKt: Intent: Intent { act=android.intent.action.SEND_MULTIPLE typ=video/mp4 flg=0x10000001 (has extras) }
2021-03-07 14:33:38.604 7396-7589/com.company.app E/DatabaseUtils: Writing exception to parcel
所以文件在那里,我在附件中看到它确实添加了它,我确实通过电子邮件/等获取了它。并且可以查看,但是日志中抛出异常。
我的 provider_paths.xml 文件如下所示:
<?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>
我的清单中有这个:
<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>
一切看起来都与我的应用程序完全一样,但这个应用程序会引发异常。
有人能看出我在做什么吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)