Android7.0文件共享问题(FileUriExposedException)
2017-04-24 13:13
399 查看
在安卓7.0以上机型,在使用
会报
异常,比如拍照时,常用的写法是:
但放在7.0以上系统就会报错了,原因我就不再讲了,详细可查一下7.0的更新内容,现在主要来提供一下简单的改进方法:
此方法只针对于对图片的处理,如果是对文件,比如更新app时,下载完app要自动进入安装界面,7.0以下的做法是:
但在7.0以上会有上述同样的问题,改进方法,使用FileProvider:
1.新建xml/file_path.xml,如图:
name为文件下载路径,如果你的app创建的文件夹为 XXApp,则name=”download”代表的路径为:XXApp/download/,path可不填。
2.在AndroidManifest.xml的application标签下添加:
3.代码中判断:
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); }
相关文章推荐
- Android7.0打开本地文件失败 android.os.FileUriExposedException
- Android7.0升级安装APK,FileUriExposedException问题
- 关于Android7.0及以上版本FileUriExposedException的问题
- android 7.0系统打开本地文件的问题android.os.FileUriExposedException:file:///storage/emulated/0/
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- Android 7.0配置fileprovider共享文件 解决FileUriExposedException
- Android7.0 FileUriExposedException
- 升级安装APK兼容Android7.0,解决FileUriExposedException
- 开发中碰到的问题:Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException: file:///storage/emulated/0/And
- android 7.0的android.os.FileUriExposedException: 问题解决
- FileUriExposedException_Android7.0适配
- Android7.0拍照失败FileUriExposedException
- 升级安装APK兼容Android7.0,解决FileUriExposedException
- Android7.0 android.os.FileUriExposedException
- Android7.0解决 android.os.FileUriExposedException: file:///storage/emulated/0/
- Android 7.0的适配问题,android.os.FileUriExposedException
- Android7.0拍照失败FileUriExposedException,你的拍照代码升级了吗
- 升级安装APK兼容Android7.0,解决FileUriExposedException
- Android7.0拍照失败FileUriExposedException异常的解决