图像意图和背面印刷

时间:2017-12-26 13:59:40

标签: android android-intent camera onbackpressed

我制作了一个图像意图,我捕捉图像并将其保存在图像视图中,但我发现如果我打开相机并按下手机上的后退按钮我的应用程序关闭并显示错误。

我的意图代码:

btnImg.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, 0);
    }
});

我的位图和imageView代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    imgBitmap = (Bitmap) data.getExtras().get("data");
    img.setImageBitmap(imgBitmap);
}

错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=0, data=null} to activity {gomugomu.civicalert/gomugomu.civicalert.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:5004)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047)
                  at android.app.ActivityThread.access$1600(ActivityThread.java:229)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7331)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                  at gomugomu.civicalert.MainActivity.onActivityResult(MainActivity.java:83)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7165)
第83行的

是:imgBitmap = (Bitmap) data.getExtras().get("data");

当我按后退按钮关闭相机并转到上一个活动

时,我希望这样

2 个答案:

答案 0 :(得分:4)

当用户成功执行操作时,resultCode将等于RESULT_OK。如果用户按下后退按钮,则resultCode将为RESULT_CANCELED。所以你的代码必须像这样修改。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK && data != null) {
            imgBitmap = (Bitmap) data.getExtras().get("data");
            img.setImageBitmap(imgBitmap);
        }
    }

答案 1 :(得分:1)

尝试使用resultCode==RESULT_OK

检查data!=nullrequest code == 0
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode == 0 && resultCode == RESULT_OK && data != null) {  
           imgBitmap = (Bitmap) data.getExtras().get("data");
           img.setImageBitmap(imgBitmap);
        }
    }
}