在我的应用程序中,我需要从图库中捕获或选择图像然后裁剪它。以下是一个示例屏幕截图:
当选择一次时,它正常工作(能够裁剪)。但是当选择始终时会出现问题:应用程序强制关闭或无法加载图像。
Logcat没有显示任何错误消息(应用程序在达到启动时会关闭)。
以下是源代码:
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");
}
}
}
}
这是否可以隐藏"始终"选项?或者还有其他解决方案吗?
答案 0 :(得分:2)
我设法用上面的答案解决了这个问题。我的所作所为意味着我只需在完整操作中删除始终和 JUST ONCE 选项。
像
this.cordova.startActivityForResult((CordovaPlugin) this,cropIntent, 2);
此行替换为
this.cordova.startActivityForResult(this,Intent.createChooser(cropIntent, "Choose App to crop "), 2);