Android 7.0上調(diào)用相機(jī)會(huì)報(bào)錯(cuò):android.os.FileUriExposedException: file:///storage/emulated/0/cache.jpg exposed beyond app through ClipData.Item.getUri()
授權(quán)的最簡(jiǎn)單方式是使用 FileProvider類(lèi)风罩。
解決辦法:
第一步:在mainfest中加入FileProvider注冊(cè)
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
第二步:配置filepaths文件煮嫌,在res目錄新建file_path.xml
其中:files-path代表的根目錄: Context.getFilesDir()
external-path代表的根目錄: Environment.getExternalStorageDirectory()
cache-path代表的根目錄: getCacheDir()
path 代表要共享的目錄
name 只是一個(gè)標(biāo)示,隨便取
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path path="" name="camera_photos" />
</paths>
</resources>
訪問(wèn)相機(jī)時(shí):
File file = new File(pathName);
Uri imageUri = FileProvider.getUriForFile(act, "包名.fileprovider", file);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent,requestCode);