Android FileUriExposedException在Oreo中
我正在尝试共享一个文本文件。我已经完成了使用FileProvider for>=Nougat共享文件所需的所有步骤,但仍然得到FileUriExposedException:Android FileUriExposedException在Oreo中,android,android-intent,android-8.0-oreo,android-fileprovider,Android,Android Intent,Android 8.0 Oreo,Android Fileprovider,我正在尝试共享一个文本文件。我已经完成了使用FileProvider for>=Nougat共享文件所需的所有步骤,但仍然得到FileUriExposedException: Fatal Exception: android.os.FileUriExposedException: file:///storage/emulated/0/abc.txt exposed beyond app through ClipData.Item.getUri() at android.os.Str
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.setFlags(intent.FLAG\授予\读取\ URI\权限)强>
如果有人能帮我解决这个问题。谢谢
编辑:
我的意图代码如下:
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\u INT>=Build.VERSION\u code.N) 目标版本需要是API 24或更高版本,而不是内部版本或android版本。
您将获得此异常,因为生成的uri仍然是file:// 参考:我以前曾提到过这一点。正如我在问题中所说,我遵循了牛轧糖及以上的文件提供商方法。。仍然面临错误我认为检查不会影响结果,因为Oreo上发生了异常。它应该使用文件提供程序uri。
/***如果这是{@code文件://}uri,它将报告给
*{@link stritmode}.**@hide*/
public void checkFileUriExposed(字符串位置){if(“file”.equals(getScheme())&&&(getPath()!=null)&&&!getPath().startsWith(“/system/”){stricmode.onFileUriExposed(this,location);}
这是uri检查失败的地方。你能记录/调试你正在生成的uri吗@苏海尔汗