Android7.0文件共享问题(FileUriExposedException)

来源:互联网 发布:php和asp哪个好 编辑:程序博客网 时间:2024/06/06 02:42

在安卓7.0以上机型,在使用

Uri uri=Uri.fromFile();

会报

android.os.FileUriExposedException: file:///storage/emulated/xxx

异常,比如拍照时,常用的写法是:

Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                Uri imageUri = Uri.fromFile(file);                openCameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                startActivityForResult(openCameraIntent, _TAKEPHOTO);

但放在7.0以上系统就会报错了,原因我就不再讲了,详细可查一下7.0的更新内容,现在主要来提供一下简单的改进方法:

         File file = new File(photoSavePath, photoSaveName);            Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {                ContentValues contentValues = new ContentValues(1);                contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());                Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);                startActivityForResult(openCameraIntent, _TAKEPHOTO);            } else {                Uri imageUri = Uri.fromFile(file);                openCameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                startActivityForResult(openCameraIntent, _TAKEPHOTO);            }

此方法只针对于对图片的处理,如果是对文件,比如更新app时,下载完app要自动进入安装界面,7.0以下的做法是:

          Uri uri = Uri.fromFile(updateFile);                    Intent intent = new Intent(Intent.ACTION_VIEW);                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                    intent.setDataAndType(uri, "application/vnd.android.package-archive");                    pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);                    ...

但在7.0以上会有上述同样的问题,改进方法,使用FileProvider:

1.新建xml/file_path.xml,如图:

这里写图片描述

<paths>    <external-path        name="download"        path="" /></paths>

name为文件下载路径,如果你的app创建的文件夹为 XXApp,则name=”download”代表的路径为:XXApp/download/,path可不填。

2.在AndroidManifest.xml的application标签下添加:

  <provider            android:name="android.support.v4.content.FileProvider"            android:authorities="你的包名"            android:grantUriPermissions="true"            android:exported="false">            <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_path" />        </provider>

3.代码中判断:

       Uri uri;                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {                        uri = FileProvider.getUriForFile(getApplicationContext(), "你的包名", updateFile);                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);                    } else {                        uri = Uri.fromFile(updateFile);                    }
3 0
原创粉丝点击