-1

I am trying to take a picture from my app using the local camera. It is taking too long to save the image in the said folder. Because of this time lag I am getting NullPointerException in my code using Bitmap library.

I have already checked for other answers related to the issue, I have provided the permissions in the AndroidManifest.xml, I have also provided Easy permission- implementation 'pub.devrel:easypermissions:0.2.1'. I am able to store the image externally but the image shows in the folder after 5-10 mins. And then the code is not able to read the image as it is not able to find it on the device. It shows up later after the app has crashed.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

        Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());
        imageView.setImageBitmap(photo);

        Intent i = new Intent(Intent.ACTION_SEND);
        Uri uri = Uri.fromFile(f);
        i.putExtra(Intent.EXTRA_STREAM, uri);
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(HomeScreen.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
        i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"abc@gmail.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");


    }
  }

The error is shown on this line imageView.setImageBitmap(photo);

D/AndroidRuntime: Shutting down VM
 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { dat=file:///storage/emulated/0/Pictures/pic/image-9747.jpg flg=0x1 }} to activity {com.example.pic/com.example.pic.HomeScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3779)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3822)
     at android.app.ActivityThread.access$1400(ActivityThread.java:155)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1429)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:152)
     at android.app.ActivityThread.main(ActivityThread.java:5497)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference```

1 Answers1

0

Have you add This on your onCreate()

imageView = (ImageView)findViewById(R.id.imageView2);

Sample ImageView declaration on Manifest.xml

 <ImageView
  android:id="@+id/uncheck_disp_act"
  android:layout_width="25dp"
  android:layout_height="25dp"
  app:srcCompat="@drawable/uncheck" />
Rubick
  • 296
  • 3
  • 22