我创建了一个应用程序,我正在打开图库,在选择图像之后,我们应该裁剪该图像并在imageview中显示,但问题是在Android 7设备裁剪中不支持错误消息。
即使曾经允许在相机和读/写外部和内部存储的运行时和清单中提供
在清单中:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.activity.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
在xml / provider_paths
中<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="."/>
</paths>
在MainActivity中: -
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_GALLERY) {
Uri selectedImage = data.getData();
performCrop(selectedImage);
} else if (requestCode == CROP_IMAGE) {
if (data != null && data.getExtras() != null) {
//save in sdcard and display in imageview
}
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "Img_" + timeStamp;
File storageDir = new File(Environment.getExternalStorageDirectory(), "/Img");
File file=new File(storageDir,imageFileName+".png");
return file;
}
private void performCrop(Uri picUri) {
try {
Uri contentUri;
if (Build.VERSION.SDK_INT > M) {
contentUri = FileProvider.getUriForFile(mContext, "com.activity.provider", createImageFile());
//TODO: Permission..
getApplicationContext().grantUriPermission("com.android.camera", contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
contentUri = picUri;
}
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(contentUri, "image");
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("scale", false);
cropIntent.putExtra("circleCrop", true);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 3);
} catch (ActivityNotFoundException anfe) {
//display an error message
ToastManager.toast(mContext, "Whoops - your device doesn't support the crop action!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
在LogCat中: -
7-06 11:04:10.053 2308-5222/? I/ActivityManager: START u0 {act=com.android.camera.action.CROP dat=content://com.ripple.activity.provider/Images/Ripple/Image/2017-07-06 11:04:10.046Ripple.jpg typ=image flg=0x3 (has extras)} from uid 10238 on display 0
07-06 11:04:10.230 2308-2798/? I/WindowManager: Destroying surface Surface(name=com.google.android.apps.photos/com.google.android.apps.photos.picker.external.ExternalPickerActivity) called by com.android.server.wm.WindowStateAnimator.destroySurface:2067 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:896 com.android.server.wm.WindowState.destroyOrSaveSurface:2094 com.android.server.wm.AppWindowToken.destroySurfaces:363 com.android.server.wm.WindowStateAnimator.finishExit:575 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:501 com.android.server.wm.WindowAnimator.updateWindowsLocked:303 com.android.server.wm.WindowAnimator.animateLocked:704
07-06 11:04:10.468 2308-5194/? I/WindowManager: Destroying surface Surface(name=com.google.android.apps.photos/com.google.android.apps.photos.picker.external.ExternalPickerActivity) called by com.android.server.wm.WindowStateAnimator.destroySurface:2067 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:896 com.android.server.wm.WindowState.removeLocked:1457 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2531 com.android.server.wm.WindowManagerService.removeWindowLocked:2489 com.android.server.wm.WindowManagerService.removeWindowLocked:2358 com.android.server.wm.WindowManagerService.removeWindow:2349 com.android.server.wm.Session.remove:193
07-06 11:04:10.495 2308-4312/? I/WindowManager: Destroying surface Surface(name=com.google.android.apps.photos/com.google.android.apps.photos.picker.impl.PickerActivity) called by com.android.server.wm.WindowStateAnimator.destroySurface:2067 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:896 com.android.server.wm.WindowState.removeLocked:1457 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2531 com.android.server.wm.WindowManagerService.removeWindowLocked:2489 com.android.server.wm.WindowManagerService.removeWindowLocked:2358 com.android.server.wm.WindowManagerService.removeWindow:2349 com.android.server.wm.Session.remove:193
07-06 11:04:10.500 23080-23080/? I/CastMediaRouteProvider: in onDiscoveryRequestChanged: request=DiscoveryRequest{ selector=MediaRouteSelector{ controlCategories=[com.google.android.gms.cast.CATEGORY_CAST/8DA7527D] }, activeScan=false, isValid=true }