-4

I am clicking Image and showing it in ImageView. below is my code. Below code execute when permission is granted.

  private void clickImage() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imageFile = new File(itemName.getText().toString()+".jpg");
    Uri tempuri = Uri.fromFile(imageFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,tempuri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
    startActivityForResult(intent,0 );
}


 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 0)
    {
        switch (resultCode){
            case Activity.RESULT_OK:
                    Log.d("path",imageFile.getAbsolutePath());
                    Toast.makeText(this,"file saved"+imageFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
                    ImageView imv = (ImageView) findViewById(R.id.itemImage);
                    Bitmap photo = (Bitmap) data.getExtras().get("data");
                    imv.setImageBitmap(photo);


                break;
            case Activity.RESULT_CANCELED:

                break;
            default:break;
        }
    }
}

Error Log.

03-01 17:51:47.183 5883-5883/com.lab.yourhomebasket E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.lab.yourhomebasket, PID: 5883
                                                                  java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.lab.yourhomebasket/com.lab.yourhomebasket.AddItemActivity}: 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:3720)
                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
                                                                      at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                                      at com.lab.yourhomebasket.AddItemActivity.onActivityResult(AddItemActivity.java:107)
                                                                      at android.app.Activity.dispatchActivityResult(Activity.java:6470)
                                                                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3716)
                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763) 
                                                                      at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

I am using moto g3 for testing. Image is not showing in my ImageView. Please help.

Deepak nigam
  • 99
  • 1
  • 15

1 Answers1

1

Quoting the documentation for ACTION_IMAGE_CAPTURE:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

You are using EXTRA_OUTPUT. Hence, data.getExtras().get("data") will not work. There does not have to even be a data Intent delivered to you if you are using EXTRA_OUTPUT.

Instead, the image should be written to the location that you provided to EXTRA_OUTPUT.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491