java.lang.NullPointerException: uri when Targeting API 33+ Camera / Image app

48 Views Asked by At

I have an app that uses a camera function to take image, or open existing library photo - the latter works fine, but the camera function, once the photo is taken, crashes out with a uri error, here is some of the code I believe can help identify the issue (hopefully!)

The error in logcat is java.lang.NullPointerException: uri

Methods to take photo to use are;

public void takePhotoFromCamera() {
    try {
        startActivityForResult(this.imageCaptureManager.dispatchTakePictureIntent(), 1);
    } catch (IOException | ActivityNotFoundException e) {
        e.printStackTrace();
    }
}



public Intent dispatchTakePictureIntent() throws IOException {
        Uri uri;
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        if (intent.resolveActivity(this.mContext.getPackageManager()) != null) {
            File createImageFile = createImageFile();
            if (Build.VERSION.SDK_INT >= 24) {
                uri = FileProvider.getUriForFile(this.mContext.getApplicationContext(), this.mContext.getApplicationInfo().packageName + ".provider", createImageFile);
            } else {
                uri = Uri.fromFile(createImageFile);
            }
            if (uri != null) {
                intent.putExtra("output", uri);
            }
        }
        return intent;
    }

public ImageCaptureManager(Context context) {
    this.mContext = context;
}

public File createImageFile() throws IOException {
    String str = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date()) + ".jpg";
    File externalStoragePublicDirectory = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    if (externalStoragePublicDirectory.exists() || externalStoragePublicDirectory.mkdir()) {
        File file = new File(externalStoragePublicDirectory, str);
        this.mCurrentPhotoPath = file.getAbsolutePath();
        return file;
    }
    Log.e("TAG", "Throwing Errors....");
    throw new IOException();
}

Error trace FATAL EXCEPTION: main

-

Process: com.thisman.socialphoto, PID: 31154
                                                                                                               java.lang.RuntimeException: Failure delivering result
   ResultInfo{who=null, request=1, result=-1, data=Intent {
   act=inline-data (has extras) }} to activity
   {com.thisman.socialphoto/com.thisman.socialphoto.activities.HomeActivity}:
   java.lang.NullPointerException: uri
                                                                                                                at
   android.app.ActivityThread.deliverResults(ActivityThread.java:5568)
                                                                                                                at
   android.app.ActivityThread.handleSendResult(ActivityThread.java:5607)
                                                                                                                at
   android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67)
                                                                                                                at
   android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
                                                                                                                at
   android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
                                                                                                                at
   android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
                                                                                                                at
   android.app.ActivityThread$H.handleMessage(ActivityThread.java:2468)
                                                                                                                at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                                at android.os.Looper.loopOnce(Looper.java:205)
                                                                                                                at android.os.Looper.loop(Looper.java:294)
                                                                                                                at android.app.ActivityThread.main(ActivityThread.java:8248)
                                                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                                                at
   com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
                                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
                                                                                                               Caused by: java.lang.NullPointerException: uri
                                                                                                                at java.util.Objects.requireNonNull(Objects.java:232)
                                                                                                                at
   android.content.ContentResolver.openInputStream(ContentResolver.java:1511)
                                                                                                                at
   com.thisman.socialphoto.activities.HomeActivity.onActivityResult(HomeActivity.java:382)
                                                                                                                at android.app.Activity.dispatchActivityResult(Activity.java:8969)
                                                                                                                at
   android.app.ActivityThread.deliverResults(ActivityThread.java:5561)
                                                                                                                at
   android.app.ActivityThread.handleSendResult(ActivityThread.java:5607) 
                                                                                                                at
   android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67) 
                                                                                                                at
   android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
                                                                                                                at
   android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139) 
                                                                                                                at
   android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) 
                                                                                                                at
   android.app.ActivityThread$H.handleMessage(ActivityThread.java:2468) 
                                                                                                                at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                                at android.os.Looper.loopOnce(Looper.java:205) 
                                                                                                                at android.os.Looper.loop(Looper.java:294) 
                                                                                                                at android.app.ActivityThread.main(ActivityThread.java:8248) 
                                                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                                at
   com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 
                                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
0

There are 0 best solutions below