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
- Android7.0文件共享问题(FileUriExposedException)
- Android7.0升级安装APK,FileUriExposedException问题
- Android7.0 FileUriExposedException
- FileUriExposedException,android7.0
- Android7.0异常:FileUriExposedException
- Android7.0文件共享
- 关于Android7.0及以上版本FileUriExposedException的问题
- Android7.0拍照失败FileUriExposedException
- Android7.0 android.os.FileUriExposedException
- Android7.0共享文件权限
- android7.0打开相册报FileUriExposedException异常
- 解决Android7.0相机 android.os.FileUriExposedException
- Android7.0相机拍照功能导致FileUriExposedException
- Android7.0须知--应用间共享文件(FileProvider)
- Android7.0须知--应用间共享文件(FileProvider)
- Android7.0须知--应用间共享文件(FileProvider)
- 解决Android7.0以上版本升级apk时报android.os.FileUriExposedException的问题
- Android7.0 应用间共享文件 FileProvider
- 2 Springboot中使用redis,配置redis的key value生成策略
- 有哪些对树莓派的有趣改造和扩展应用?
- tableView的性能优化
- a girl i love
- R语言读取空间数据以及ArcGIS中OLS工具回归结果可视化R语言版
- Android7.0文件共享问题(FileUriExposedException)
- nginx localhost 跨域配置,限制header,限制提交方式method
- ios phonegap入门,可以帮你省掉不少麻烦
- 棋盘问题 (dfs)
- 今天偶然发现了一个免费云SVN平台,国内的
- Android 图片添加水印
- 非系统数据文件损坏,rman备份恢复
- Elasticsearch分页查询From&Size VS scroll
- 天易44------java实现对文件MD5校验