美文网首页Android开发Android技术知识
解决android.os.FileUriExposedExcep

解决android.os.FileUriExposedExcep

作者: itgoyo简书 | 来源:发表于2017-04-25 11:34 被阅读0次

今天在使用Android7.0手机升级app的时出现了该问题,然后上StackOverFlow查了一下,找到了几种方法,推荐使用第二种,简单快捷

The app crashes when trying to open a file. It work below Android N, but on Android N it crashes. It only crashes when I try to open a file from the SD card, not from the system partition.

解决方法 ①:

If your targetSdkVersion is 24 or higher, we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.

Steps to replace file:// uri with content:// uri:

  • add a FileProvider tag in AndroidManifest.xml under tag.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name="android.support.v4.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>
    </application>
</manifest>

  • then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>
  • The final step is to change the line of code below in
Uri photoURI = Uri.fromFile(createImageFile());

to

Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());

解决方法 ②:

Besides the solution using the FileProvider, there is another way to work around this. use this codes in your project application onCreate();

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure()

May be wrong in the third line

you can run like this:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.detectFileUriExposure();
        }

StackOverFlow:
http://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

相关文章

  • 解决android.os.FileUriExposedExcep

    今天在使用Android7.0手机升级app的时出现了该问题,然后上StackOverFlow查了一下,找到了几种...

  • 【解决】

    解决理想 解决生活 解决让我平凡的错 解决孤单 解决折磨 解决一开始就着了的魔 解决饥饿 解决战火 解决核时代被抽...

  • 报错集锦

    问题一: 解决: 问题二: 解决: 问题三: 解决: 问题四: 解决: 问题五: 解决:

  • 解决 问题 解决

    你解决一个问题,就像当于解决无数个问题。听到这句话的时候我已经毕业了。回想之前的学习经历,一张试卷,碰到难题就放那...

  • some

    枪解决了, 炮解决了, 人解决了, 衣服解决了, 时间解决了, 地方解决了, 就看着车马飞驰, 结局降临, 吾事已毕。

  • 我还是在这里

    生活不能解决的事情,日记可以解决;日记不能解决的事情,思想可以解决;思想不能解决的事情,放空可以解决;放...

  • 页面布局(三栏布局)

    浮动解决方案 绝对定位解决方案 flexbox解决方案 表格布局解决方案 网格布局解决方案

  • 忙起来

    忙碌,能解决很多问题 解决你胡思乱想的问题 解决你彷徨迷茫的问题 解决你痴心妄想的问题 解决你自卑弱小的问题 解决...

  • ……纵情沉浸于清愁与静谧吧

    生活不能解决的事情,日记可以解决;日记不能解决的事情,思想可以解决;思想不能解决的事情,放空可以解决;放空不...

  • 《慧杰语录》——每日必读

    劳动,解决吃饭的问题。 工作,解决生存的问题。 能力,解决存在的问题。 素质,解决关系的问题。 奋斗,解决态度的问...

网友评论

    本文标题:解决android.os.FileUriExposedExcep

    本文链接:https://www.haomeiwen.com/subject/yfcvzttx.html