use*_*654 10 android android-fileprovider
我已经审阅了几篇关于此问题的帖子,并且尝试了六种建议的解决方案。我有一些与此工作几乎相同的东西,而不会在我编写的另一个应用程序中引发异常,但我只是无法让它不引发异常,尽管文件确实被传输了!正在设置权限并且文件传输得很好,但仍然会抛出异常,尽管它不会使应用程序崩溃。
我所拥有的是这样的:
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}")
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪如下所示:
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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
所以文件就在那里,我在附件中看到它确实添加了它,并且我确实通过电子邮件/等方式获取了它。并可以查看它,但日志中抛出异常。
我的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>
Run Code Online (Sandbox Code Playgroud)
我的清单中有这样的内容:
<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>
Run Code Online (Sandbox Code Playgroud)
一切看起来都和我的应用程序一模一样,但这个应用程序抛出了异常。
任何人都可以看到我所做的事情有什么问题吗?
使用ShareCompat.IntentBuilder。它正在为您做所有正确的事情。有关根本问题,请参阅https://issuetracker.google.com/issues/173137936
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 uri = FileProvider.getUriForFile(context, context.packageName + ".provider", videoFile)
ShareCompat.IntentBuilder(context)
.setType("video/mp4")
.setSubject("Shared files")
.addStream(uri)
.setChooserTitle("Shared Videos")
.startChooser()
}
Run Code Online (Sandbox Code Playgroud)
小智 2
在这里检查这个/sf/answers/4160752151/
您需要向可用于此意图的每个可用应用程序授予权限。
| 归档时间: |
|
| 查看次数: |
7627 次 |
| 最近记录: |