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.