我有一个完美运行的 PerformCrop 方法。
我想把它移到我的“util”类中,这样我就可以在其他活动中调用它。这有一些复杂性,因为它使用返回调用类的意图。我认为这应该没有问题,但也许我不正确。
这是我尝试过的以及我看到的问题
- 我将 performCrop() 剪切并粘贴到我的 util 类中。
- 我在“startActivityForResult(cropIntent, CROP_IMAGE);”出现错误 它没有识别该方法,因此我使用“扩展活动”扩展了该类
- 当它运行时出错,我不是 100% 确定原因,但捕获到异常
'java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()'
这是方法,下面是“onActivityResult”方法。谁能告诉我如何将 performCrop() 移动到非 Activity 扩展类?
代码
public void performCrop(Uri picUri)
{
final int CROP_IMAGE = 2;
/*
* PERFORM CROP
* this must take into account he fact we may not have a crop method on phone, or the diff ways diff OS crop
*/
//NEXUS 5 OS 5 is example of this branch
// take care of exceptions
try {
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// 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
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe)
{
Toast toast = Toast.makeText(this,"This device doesn't support the crop action! Exception: " + anfe.toString(),Toast.LENGTH_SHORT);
toast.show();
}
catch (OutOfMemoryError e)
{//NOT TESTED AS HW DOES NOT GO HERE
System.out.println("out of memory");
}
catch (Exception e)
{ //NOT TESTED AS HW DOES NOT GO HERE
Display display = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
//display.getSize(size);
int width = size.x;
int height = size.y;
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// // indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
// // indicate output X and Y
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", 200);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
}
}
当相机意图返回成功结果时从 Activity 调用。请注意,裁剪意图也将返回此方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
if (resultCode==RESULT_OK )
{
if(requestCode == CAMERA_IMAGE) //reply from camera
{
toolbox.performCrop(uri); //crop the picture
}
if(requestCode == CROP_IMAGE) //reply from crop
{
/* Bitmap bmp = getBitmap(uri);
imgView.setImageBitmap(bmp);*/
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap bmp = extras.getParcelable("data");
imgViewProfilePic.setImageBitmap(bmp);
}
}
if (requestCode == PICK_IMAGE_FROM_GALLERY) //reply from gallery
{
Uri selectedImage = data.getData();
uri = data.getData();
toolbox.performCrop(uri);
}
}
}