Android在7.0以后傳遞Uri
直接和低版本一樣操作會報FileUriExposedException
,涉及到了數(shù)據(jù)共享問題拐袜,具體實現(xiàn):
首先在AndroidManifest
中添加如下:
</application>
...
<!--文件共享-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
在屬性android:authorities
中值填寫為包名+fileprovider
吉嚣,然后在meta-data
中的resource
中對應(yīng)相關(guān)的xml文件,創(chuàng)建該文件并填寫內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<path>
<external-path
name="my_images"
path=""/>
</path>
其中
- <files-path/> //代表的根目錄: Context.getFilesDir()
- <external-path/> //代表的根目錄: Environment.getExternalStorageDirectory()
- <cache-path/> //代表的根目錄: getCacheDir()
name 可以隨便填寫蹬铺,path為共享空間尝哆,不填為整個根目錄,進入代碼模塊:
定義常量:
private final int TAKE_PHOTO_CODE = 1000;// 拍照
private final int SELECT_PHOTO_CODE = 1001;// 圖庫
private final int CUT_PICTURE_CODE = 1002;// 裁剪
private File file;// 拍的照片
private String filePath = Constant.TAKE_PHOTO_PATH;// 拍照的原圖地址
private File cropFile;// 剪切后的圖片
private String cropPath = Constant.CUT_PHOTO_PATH;// 剪切的原圖地址
然后初始化文件甜攀,如果已經(jīng)存在要刪除后在創(chuàng)建:
file = new File(filePath);
cropFile = new File(cropPath);
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
if (cropFile.exists()) {
cropFile.delete();
}
cropFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
開始拍照秋泄,在拍照之前請檢查權(quán)限,確認(rèn)擁有相機權(quán)限之后調(diào)用如下方法:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri temp;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
temp = FileProvider.getUriForFile(ContextHandler.currentActivity(), "包名.fileprovider", file);// 一定要對應(yīng)AndroidMainfast中配置的值
} else {
temp = Uri.fromFile(file);
}
// 啟動相機程序
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, temp);
startActivityForResult(intent, TAKE_PHOTO_CODE);
調(diào)用圖庫就簡單多了:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, SELECT_PHOTO_CODE);
然后是裁剪的方法:
public void startPhotoZoom(Uri uri) {// 這里的uri也要經(jīng)過處理
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); //添加這一句表示對目標(biāo)應(yīng)用臨時授權(quán)該Uri所代表的文件
}
intent.setDataAndType(uri, "image/*");
// 下面這個crop=true是設(shè)置在開啟的Intent中設(shè)置顯示的VIEW可裁剪
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra("aspectX", 1);// 比例
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);// 輸出大小
intent.putExtra("outputY", 300);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropFile));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, CUT_PICTURE_CODE);
}
拍照规阀、選圖恒序、裁圖返回來的處理:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == TAKE_PHOTO_CODE) {// 拍照返回
Uri mUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {// 處理uri
mUri = FileProvider.getUriForFile(this, "com.nuhtech.cmedicine.fileprovider", file);
}else{
mUri = Uri.fromFile(file);
}
startPhotoZoom(mUri); //進行裁剪
} else if (resultCode == Activity.RESULT_OK && requestCode == SELECT_PHOTO_CODE) {// 選圖返回
if (data != null) {
startPhotoZoom(data.getData());//進行裁剪
}
} else if (resultCode == Activity.RESULT_OK && requestCode == CUT_PICTURE_CODE) {// 裁圖返回
// 現(xiàn)在已經(jīng)裁剪完畢,根據(jù)需求處理結(jié)果即可:
// cropFile 剪切后的圖片
// cropPath 剪切的原圖地址
// 圖片上傳完成之后記得刪除文件
file.delete();
cropFile.delete();
}
}