【发布时间】:2017-09-09 11:24:49
【问题描述】:
我尝试实现帮助用户捕获照片并裁剪或选择图像并裁剪它的应用程序,该应用程序在 android 4.4 中成功运行,但是当尝试在另一台设备 5.1 中对其进行测试时,裁剪图像后出现对话框并选择保存带有以下消息的选项unfortunately gallery has stopped
下面的代码我是如何开始裁剪的。
public void ImageCropFunction() {
try {
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(uri, "image/*");
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent, 1);
} catch (ActivityNotFoundException e) {
}
}
用于从相机拍照
public void ClickImageFromCamera() {
CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
"file" + String.valueOf(System.currentTimeMillis()) + ".jpg");
uri = Uri.fromFile(file);
CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
CamIntent.putExtra("return-data", true);
startActivityForResult(CamIntent, 0);
}
从图库中挑选图片
public void GetImageFromGallery() {
GalIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(GalIntent, "Select Image From Gallery"), 2);
}
并使用
请求运行时权限public void EnableRuntimePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(StartUp.this,
Manifest.permission.CAMERA)) {
Toast.makeText(StartUp.this, "CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(StartUp.this, new String[]{
Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,}, RequestPermissionCode);
}
}
并用于获取选择图像或请求权限的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
ImageCropFunction();
} else if (requestCode == 2) {
if (data != null) {
uri = data.getData();
ImageCropFunction();
}
} else if (requestCode == 1) {
if (data != null) {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
this.bitmap = bitmap;
imageView.setImageBitmap(bitmap);
buttonDetect.setVisibility(View.VISIBLE);
}
}
}
发生崩溃后Logcat不显示任何问题只显示这个
START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView
谁能帮忙?
【问题讨论】:
-
catch (ActivityNotFoundException e) { }?空的?将通常的 e.printStackTrace() 和 Toast() 放入其中,您可以在其中向用户显示 e.getMessage()。现在您的用户和您不知道发生了什么。 -
and for get result of pick image or request permission不能使用 onActivityResult 获取权限。如果你得到一个,它不会被调用。改为实现 onRequestPermissionsResult()。 -
@greenapps for
catch (ActivityNotFoundException e) { }我试了一下,但没有出现在问题中,吐司也没有出现。获得许可我的意思是检查是否授予许可 -
the application show Gallery has stopped after crop the image抱歉,我无法关注你。你想表达什么?请更好地制定。调整您的帖子。 -
@greenapps 对话框在裁剪图像后出现并选择保存并显示以下消息
unfortunately gallery has stopped