Android 7.0+相机、相册、裁剪适配问题
在manifest中:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.***.***.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
1、android:authorities:可以随便取,在之后的代码需要一一对应:
2、android:resource:创建一个xml文件夹,xml文件夹下创建file_paths资源文件
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="" name="camera_photos" />
<files-path path="" name="photos" />
</paths>
</resources >
代码中:
private String mPhotoPath;
private Bitmap mBitmap;
private File mAvatarFile;
private File fileUri;
/**
* 调用手机相机
*/
private void getImageFromPhoto() {
File file = new File(Constants.APP_PATH);
if (!file.exists()) {
file.mkdirs();
}
mPhotoPath = Constants.APP_PATH + String.valueOf(System.currentTimeMillis()) + ".jpg";
fileUri = new File(mPhotoPath);
Intent intentCamera = new Intent();
Uri imageUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
imageUri = FileProvider.getUriForFile(ChangeUserInfoActivity.this, "com.***.***.fileprovider", fileUri);
} else {
imageUri = Uri.fromFile(fileUri);
}
intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intentCamera, START_PHOTO_CAMERA_REQUEST);
}
/**
* 调用手机相册
*/
private void allPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, START_PHOTO_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == START_PHOTO_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
//调用相册
Cursor cursor = getContentResolver().query(data.getData(), new String[]{MediaStore.Images.Media.DATA}, null, null, null);
//游标移到第一位,即从第一位开始读取
if (cursor != null) {
cursor.moveToFirst();
mPhotoPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
//调用系统裁剪
mAvatarFile = new File(mPhotoPath);
final Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, "com.***.***.fileprovider", mAvatarFile);
} else {
uri = Uri.fromFile(mAvatarFile);
}
startPhoneZoom(uri);
}
} if (requestCode == START_PHOTO_CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
//相机返回结果,调用系统裁剪
mAvatarFile = new File(mPhotoPath);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, "com.***.***.fileprovider", mAvatarFile);
} else {
uri = Uri.fromFile(mAvatarFile);
}
startPhoneZoom(uri);
}
if (requestCode == START_CROP_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
//设置裁剪返回的位图
Bundle bundle = data.getExtras();
if (bundle != null) {
mBitmap = bundle.getParcelable("data");
SDLogUtil.d(TAG, "--------------bitmap------------" + mBitmap);
}
}
}
/**
* 调用系统裁剪的方法
*/
private void startPhoneZoom(Uri uri) {
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(uri, "image/*");
//是否可裁剪
intent.putExtra("corp", "true");
//裁剪器高宽比
intent.putExtra("aspectY", 1);
intent.putExtra("aspectX", 1);
//设置裁剪框高宽
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
//返回数据
intent.putExtra("return-data", true);
startActivityForResult(intent, START_CROP_IMAGE_REQUEST);
}