gpt4 book ai didi

android - 奥利奥中的 FileUriExposedException

转载 作者:行者123 更新时间:2023-11-29 23:33:04 27 4
gpt4 key购买 nike

我正在尝试共享一个文本文件。我已经按照所有必需的步骤使用 FileProvider 共享文件 >=Nougat 但仍然收到 FileUriExposedException :

Fatal Exception: android.os.FileUriExposedException: file:///storage/emulated/0/abc.txt exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1975)
at android.net.Uri.checkFileUriExposed(Uri.java:2363)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:941)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9944)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9950)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9929)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1622)
at android.app.Activity.startActivityForResult(Activity.java:4751)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4691)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:5112)
at android.app.Activity.startActivity(Activity.java:5080)
at com.abc.sendChatThroughEmail(ConversationActivity.java:841)
at com.abc.onOptionsItemSelected(ConversationActivity.java:752)
at android.app.Activity.onMenuItemSelected(Activity.java:3696)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:679)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
at android.widget.AdapterView.performItemClick(AdapterView.java:321)
at android.widget.AbsListView.performItemClick(AbsListView.java:1217)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3203)
at android.widget.AbsListView$3.run(AbsListView.java:4151)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

我的分享代码是:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
try {
String packageId = context.getPackageName();
return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file);
} catch(IllegalArgumentException e) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
throw new SecurityException();
} else {
return Uri.fromFile(file);
}
}
} else {
return Uri.fromFile(file);
}

并且还添加了对 Intent 的许可:

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

如果有人能帮我解决这个问题。谢谢。

编辑:我的 Intent 代码如下:

intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "abc");
try {
intent.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, chatFile));
} catch (SecurityException e) {
Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, chatFile.getAbsolutePath()), Toast.LENGTH_SHORT).show();
return null;
}
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

最佳答案

您不需要此检查 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 。

它是目标版本,需要是 API 24 或更高版本,而不是构建或 android 版本。您收到此异常是因为生成的 uri 仍然是 file://。

关于android - 奥利奥中的 FileUriExposedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com