最近開(kāi)發(fā)項(xiàng)目遇到7.0 拍照崩潰記錄此問(wèn)題
這個(gè)異常只會(huì)在Android 7.0+ 上會(huì)出現(xiàn)此問(wèn)題妆距,當(dāng)app使用file:// url 共享給其他app時(shí)矢空, 會(huì)拋出這個(gè)異常
官方推薦使用FileProvider 來(lái)解決此問(wèn)題
第一步在manifest.xml文件添加provider,相機(jī)舍杜,讀寫(xiě)文件權(quán)限
-
第二步在appliction 節(jié)點(diǎn)中插入代碼,注意 android:authorities 里面的值貌笨,在后面使用的getUriForFile(Context, String, File) 中, 第二個(gè)參數(shù)就是這個(gè)里面的值绑青,請(qǐng)務(wù)必填寫(xiě)正確。
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.example.android.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"></meta-data> </provider>
- 第三步在res節(jié)點(diǎn)下創(chuàng)建xml包并添加provider_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
paths 可配置選項(xiàng)
<files-path name="name" path="path" /> //相當(dāng) Context.getFilesDir() + path, name是分享url的一部分
<cache-path name="name" path="path" /> //getCacheDir()
<external-path name="name" path="path" /> //Environment.getExternalStorageDirectory()
<external-files-path name="name" path="path" />//getExternalFilesDir(String) Context.getExternalFilesDir(null)
<external-cache-path name="name" path="path" /> //Context.getExternalCacheDir()
- 第4步在activity中調(diào)用拍照版本高于或等于android 7.0使用FileProvider.getUriForFile 否則使用Uri.fromFile(file);
/**
* 打開(kāi)系統(tǒng)相機(jī)
*/
private void openCamera() {
File file = new FileStorage().createIconFile();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//通過(guò)FileProvider創(chuàng)建一個(gè)content類(lèi)型的Uri ,和清單文件保持一致
imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", file);
} else {
imageUri = Uri.fromFile(file);
}
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
}
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設(shè)置Action為拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片保存到指定URI
startActivityForResult(intent, REQUEST_CAPTURE);
}
public File createIconFile() {
String fileName = "";
if (iconDir != null) {
fileName = UUID.randomUUID().toString() + ".png";
}
return new File(iconDir, fileName);
}
- File file = new FileStorage().createIconFile();
創(chuàng)建文件
public class FileStorage {
private File cropIconDir;
private File iconDir;
public FileStorage() {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File external = Environment.getExternalStorageDirectory();
String rootDir = "/" + "image";
cropIconDir = new File(external, rootDir + "/crop");
if (!cropIconDir.exists()) {
cropIconDir.mkdirs();
}
iconDir = new File(external, rootDir + "/icon");
if (!iconDir.exists()) {
iconDir.mkdirs();
}
}
}
public File createCropFile() {
String fileName = "";
if (cropIconDir != null) {
fileName = UUID.randomUUID().toString() + ".png";
}
return new File(cropIconDir, fileName);
}
public File createIconFile() {
String fileName = "";
if (iconDir != null) {
fileName = UUID.randomUUID().toString() + ".png";
}
return new File(iconDir, fileName);
}
}
- 裁剪
/**
* 裁剪
/
private void cropPhoto() {
File file = new FileStorage().createCropFile();
Uri outputUri = Uri.fromFile(file);//縮略圖保存地址
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(imageUri, "image/");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, REQUEST_PICTURE_CUT);
}
- 第5步 activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//此處為系統(tǒng)拍照截圖返回
cameraProxy.onResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CAPTURE://拍照
if (resultCode == RESULT_OK) {
cropPhoto();
}
break;
case REQUEST_PICTURE_CUT://裁剪完成
spUtil.setUserHead(imageUri.getPath());
break;
}
}