重构活动类

时间:2015-09-02 11:41:40

标签: android android-activity crop android-crop

我有一个完美的PerformCrop方法。

我想把它转移到我的' util'所以我可以在其他活动中称呼它。这有一个复杂因素,它使用返回调用类的意图。我认为这不应该是问题,但也许我不对。

以下是我所尝试的以及我所看到的问题

  1. 我将performCrop()剪切并粘贴到我的util类中。
  2. 我在'startActivityForResult(cropIntent,CROP_IMAGE)中遇到错误;'它没有识别方法,所以我用“扩展活动”
  3. 扩展了课程
  4. 当它运行时出现错误并且我不是100%确定为什么会捕获异常
  5.   

    ' java.lang.NullPointerException:尝试调用虚方法   ' android.app.ActivityThread $ ApplicationThread   android.app.ActivityThread.getApplicationThread()'在null对象上   参考'

    以下是方法,即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);
            }
        }
    }
    

2 个答案:

答案 0 :(得分:1)

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

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

CropImage claas看起来像,

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

答案 1 :(得分:0)

首先检查哪里结果

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

如果Getpaprent()方法调用则结果发送到父活动。 所以得到父活动的结果并重新发送到你的活动.....