Android7.0拍照失败FileUriExposedException,你的拍照代码升级了吗

背景:

SamSung SM-N9006 Android5.0在应用中拍照之后,无法获取拍照之后的数据,报错FileUriExposedException

思路:

参考官方文档对该错误的解释,是由于出于安全考虑,Android 7.0[API24]以及以上版本不支持file://,使用content://URI,可能三星这款机型动了Framework吧。

Note: We are using getUriForFile(Context, String, File) which returns a content:// URI. For more recent apps targeting Android 7.0 (API level 24) and higher, passing a file:// URI across a package boundary causes a FileUriExposedException. Therefore, we now present a more generic way of storing images using a FileProvider.

关键方法:

1.启动照相时,借助FileProvider生成content://URI保存拍照结果
老方法

    /**
     * 老方法[Android7.0以及以上报错FileUriExposedException]
     */
    private void doTakePhotoOld() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            File newFile = createTakePhotoFile();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newFile));
            startActivityForResult(intent, REQUEST_CAMERA);
        }
    }

新方法

    /**
     * 解决三星手机拍照后无法获取数据,android 7.0手机拍照后获取数据适配。
     * 报错FileUriExposedException(SamSung SM-N9006 Android5.0也有这问题)
     */
    private void doTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager())!=null){
            midFile = null;
            try {
                midFile = createImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (midFile!=null){
                Uri photoURI = FileProvider.getUriForFile(this,FILE_AUTHORITY,midFile);
                // fix java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{42725078 24872:com.android.camera/u0a14} (pid=24872, uid=10014) that is not exported from uid 10310
                List<ResolveInfo> resInfoList= getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    grantUriPermission(packageName, photoURI,
                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }
                // fix java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{42725078 24872:com.android.camera/u0a14} (pid=24872, uid=10014) that is not exported from uid 10310
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                this.startActivityForResult(takePictureIntent, GetByCamre);
            }
        }
    }

    /**
     *storage/emulated/0/.REAPP/._Image:SDCard路径加上.REAPP/._Image/temp.img
     * @return  接收拍照返回数据位置,
     * @throws IOException
     */
    private File createImageFile() throws IOException {
        File dir = new File(SuFileInfo.getInstance().getImagePath());
        if (!dir.exists()){
            dir.mkdirs();
        }
        // 以上为了规避用户删除了._Image目录,报错java.io.IOException: open failed: ENOENT (No such file or directory)
        File file = new File(dir, TempImageMame);
        if (!file.exists()){
            file.createNewFile();
        }
        return file;
    }

小米复现java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{42725078 24872:com.android.camera/u0a14} (pid=24872, uid=10014) that is not exported from uid 10310,此时需要for循环授权进行修复。

2.对FileProvider进行设置
2.1AndroidManifest.xml注册

<application ... 
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.harry.shopping.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>
    ...
</application>

注意,android:authorities属性值和之前FileProvider.getUriForFile方法使用的authorities必须保持一致。
2.2在res/xml新建file_paths.xml设置文件路径

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="images" path="Android/data/com.harry.shopping/files/Pictures" />
</paths>

经过以上操作就可以在onActivityResult里面获取到照片路径mCurrentPhotoPath。

附录1:使用时FileProvider五个步骤

1.定义一个FileProvider,并在AndroidManifest.xml注册。一般v4包下的的FileProvider即可

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.harry.shopping.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

2.Provider配置文件路径
2.1配置meta-data指定保存文件路径

<meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_paths"/>

2.2在xml文件下新建file_paths配置路径

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="images" path="Android/data/com.harry.shopping/files/Pictures" />
</paths>

name表示生成URI时的别名,path是指相对路径

<files-path name="name" path="path" />
Context.getFilesDir()
<cache-path name="name" path="path" />
Context.getCacheDir()
<external-path name="name" path="path" />
Environment.getExternalStorageDirectory()
<external-files-path name="name" path="path" />
Context#getExternalFilesDir(String) Context.getExternalFilesDir(null).
<external-cache-path name="name" path="path" />
Context.getExternalCacheDir()

在使用external-files-path和external-cache-path需要注意,这两个是在support v4 24以上FileProvider才添加的

private static final String TAG_EXTERNAL_FILES = "external-files-path";
private static final String TAG_EXTERNAL_CACHE = "external-cache-path";

3.为一个文件生成Content URI

File imagePath = new File(Context.getFilesDir(), "images");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

生成URI:content://com.mydomain.fileprovider/my_images/default_image.jpg4.为URI临时授权,两种方法

1)Context.grantUriPermission(package, Uri, mode_flags);
// mode_flags可以设置为 FLAG_GRANT_READ_URI_PERMISSION, FLAG_GRANT_WRITE_URI_PERMISSION

权限失效:用户取消权限[revokeUriPermission() ]或者手机重启

2)Intent intent = new  Intent();
intent.setData(Uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
setResult(RESULT_OK,intent);

权限失效:返回处理结果Activity所在的stack结束
5.发送这个URI给其他APP,两种方法 1)startActivityResult() 2)借助ClipData处理

附录2:最近路径容易搞混,也打印记录下

Context.getFilesDir()=/data/data/com.harry.shopping/files
Context.getCacheDir()=/data/data/com.harry.shopping/cache
Environment.getExternalStorageDirectory()=/storage/emulated/0
getExternalFilesDir(Environment.DIRECTORY_PICTURES)=/storage/emulated/0/Android/data/com.harry.shopping/files/Pictures
Context.getExternalFilesDir(null)=/storage/emulated/0/Android/data/com.harry.shopping/files
Context.getExternalCacheDir()=/storage/emulated/0/Android/data/com.harry.shopping/cache

总结

这类问题比较简单,查看报错Log,参考官方文档,借助FileProvider可以方便的处理这类安全问题。

参考文献:

https://developer.android.com/training/camera/photobasics.html
https://developer.android.com/reference/android/support/v4/content/FileProvider.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,577评论 25 707
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,214评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,103评论 18 139
  • Android7.0发布已经有一个多月了,Android7.0在给用户带来一些新的特性的同时,也给开发者带来了新的...
    东经315度阅读 1,309评论 0 14
  • 不能相信任何人皮面具,跟着感觉走
    游离婷阅读 118评论 0 0