在我的应用程序中,我需要从图库中捕获或选择图像,然后裁剪它。以下是示例屏幕截图:
当选择仅一次时,它会正常工作(能够裁剪)。但当选择始终时,就会出现问题:应用强制关闭或无法加载图像。
Logcat 没有显示任何错误消息(应用程序在到达 startactivity 时强制关闭)。
这是源代码:
public class CropImage extends CordovaPlugin{
public final String ACTION_GET_IMAGE_NAME = "GetImageName";
Uri myUri;
int RESULT_CANCELED = 0;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
boolean result = false;
if(action.equals(ACTION_GET_IMAGE_NAME)) {
try {
myUri = Uri.parse(args.getString(0));
cropCapturedImage(myUri);
} catch (JSONException e) {
e.printStackTrace();
}
result = true;
}
return result;
}
public void cropCapturedImage(Uri picUri){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri of image
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
String[] separated = String.valueOf(picUri).split("/");
File f = new File(Environment.getExternalStorageDirectory()+"/novema/files/ski/"+separated[7]);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
cropIntent.putExtra("output", Uri.fromFile(f));
cropIntent.putExtra("return-data", false);
//start the activity - we handle returning in onActivityResult
this.cordova.startActivityForResult((CordovaPlugin) this,cropIntent, 2);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(resultCode != RESULT_CANCELED && intent != null){
if(requestCode == 2){
Bundle extras = intent.getExtras();
//get the cropped bitmap from extras
Bitmap thePic = extras.getParcelable("data");
}
}
}
}
有什么方法可以隐藏“始终”选项吗?或者还有其他解决办法吗?
最佳答案
我设法使用上述答案解决了该问题。我所做的意味着我只是在完整操作中删除始终和仅一次选项。
喜欢
this.cordova.startActivityForResult((CordovaPlugin) this,cropIntent, 2);
这行替换成这样
this.cordova.startActivityForResult(this,Intent.createChooser(cropIntent, "Choose App to crop "), 2);
关于android - 在 "Always"对话框中选择 "Complete action using"选项时,应用程序强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397707/