Oreo中的FileUriExposedException

时间:2018-09-25 09:44:19

标签: android android-intent android-8.0-oreo android-fileprovider

我正在尝试共享文本文件。我已经按照所有必需的步骤使用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.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

如果有人可以帮助我解决此问题。谢谢。

编辑: 我的意图代码如下:

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);

1 个答案:

答案 0 :(得分:1)

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

目标版本需要为API 24或更高版本,而不是内部版本或android版本。 由于正在生成的uri仍然是file://,因此您将收到此异常。