gpt4 book ai didi

android - 从 Activity 类中重构 performCrop() 方法

转载 作者:行者123 更新时间:2023-11-30 01:55:15 25 4
gpt4 key购买 nike

我有一个完美运行的 PerformCrop 方法。

我想将它移到我的“util”类中,以便我可以在其他 Activity 中调用它。这有一些复杂性,因为它使用返回调用类的 Intent 。我认为这应该没问题,但也许我不正确。

这是我尝试过的以及我遇到的问题

  1. 我将 performCrop() 剪切并粘贴到我的实用程序类中。
  2. 我在“startActivityForResult(cropIntent, CROP_IMAGE);”处出错它没有识别该方法,所以我用“扩展 Activity ”扩展了类
  3. 当它运行时出错,我不是 100% 确定原因,但捕获了一个异常

'java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference'

这是方法,下面是 '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);
}
}

当相机 Intent 返回成功结果时从 Activity 调用。请注意,裁剪 Intent 也将返回到此方法。

@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);
}
}
}

最佳答案

创建一个名为 CropImage 的类,将您的方法 performCrop() 放入其中,然后从您想要的任何地方调用它,

                CropImage cropImage = new CropImage(this);
cropImage.performCrop(uri);

CropImage 类看起来像,

public class CropImage {
Context context;

public CropImage(Context context) {
this.context = context;
}

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
((Activity)context).startActivityForResult(cropIntent, CROP_IMAGE);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe)
{
Toast toast = Toast.makeText(context,"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 = ((Activity)context).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
((Activity)context).startActivityForResult(cropIntent, CROP_IMAGE);
}
}
}

关于android - 从 Activity 类中重构 performCrop() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32352198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com