从 activity class 中重构 performCrop() 方法

refactor performCrop() method out of activity class

我有一个完美的 PerformCrop 方法。

我想将它移动到我的 'util' class 以便我可以在其他活动中调用它。这有一些复杂性,因为它使用 return 调用 class 的意图。我认为这应该没问题,但也许我不正确。

这是我尝试过的方法和我遇到的问题

  1. 我将 performCrop() 剪切并粘贴到我的实用程序 class。
  2. 我在 'startActivityForResult(cropIntent, CROP_IMAGE);' 处遇到错误,它无法识别该方法,因此我将 class 扩展为 'Extends 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 扩展 class?

代码

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

当相机意图 return 成功时从 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);
        }
    }
}

创建一个 Class 名称 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);
    }
}
}

首先检查结果的去向

ifgetpaternt==null)
{
Startactivityforreslt(your intent,code);
}
else
{
getpaprent().Startactivityforreslt(your intent,code);
}

If Getpapren() 方法调用 Then result send to parent activity . 所以在父级 activity 中获取结果并重新发送到您的 activity .....