{"record":{"id":"cdc46d939eb19d55","repo":"Yalantis/uCrop","slug":"output-uri-is-null-cannot-copy-image","errorCode":null,"errorMessage":"Output Uri is null - cannot copy image","messagePattern":"Output Uri is null - cannot copy image","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java","lineNumber":174,"sourceCode":"        } else if (isContentUri(mInputUri)) {\n            try {\n                copyFile(mInputUri, mOutputUri);\n            } catch (NullPointerException | IOException e) {\n                Log.e(TAG, \"Copying failed\", e);\n                throw e;\n            }\n        } else if (!isFileUri(mInputUri)) {\n            String inputUriScheme = mInputUri.getScheme();\n            Log.e(TAG, \"Invalid Uri scheme \" + inputUriScheme);\n            throw new IllegalArgumentException(\"Invalid Uri scheme\" + inputUriScheme);\n        }\n    }\n\n    private void copyFile(@NonNull Uri inputUri, @Nullable Uri outputUri) throws NullPointerException, IOException {\n        Log.d(TAG, \"copyFile\");\n\n        if (outputUri == null) {\n            throw new NullPointerException(\"Output Uri is null - cannot copy image\");\n        }\n\n        InputStream inputStream = null;\n        OutputStream outputStream = null;\n        try {\n            inputStream = mContext.getContentResolver().openInputStream(inputUri);\n            if (inputStream == null) {\n                throw new NullPointerException(\"InputStream for given input Uri is null\");\n            }\n\n            if (isContentUri(outputUri)) {\n                outputStream = mContext.getContentResolver().openOutputStream(outputUri);\n            } else {\n                outputStream = new FileOutputStream(new File(outputUri.getPath()));\n            }\n\n            byte buffer[] = new byte[1024];\n            int length;","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java#L156-L192","documentation":"BitmapLoadTask.copyFile throws this NullPointerException when asked to copy an image but the destination output Uri is null. The output Uri is a required argument of UCrop.of(); a null value makes the copy step meaningless, so the task fails immediately inside doInBackground.","triggerScenarios":"Invoking uCrop with a null destination Uri (UCrop.of(sourceUri, null)) or a null mOutputUri reaching the copy branch of processInputUri (e.g. for http(s) sources that need download-to-file, or non-content non-http sources that need copying).","commonSituations":"Building the destination Uri from a variable that failed initialization or from an unresolvable cache path returning null; calling UCrop.of before the save-file path is computed (async null).","solutions":["Always pass a non-null destination Uri to UCrop.of(), e.g. Uri.fromFile(new File(getCacheDir(), \"cropped.jpg\")).","Ensure the out-uri extra (UCrop.EXTRA_OUTPUT_URI) is actually populated when constructing options programmatically.","Null-check the destination before launching the crop intent and bail early with user feedback.","If the path comes from getExternalFilesDir or similar, handle its null return (storage not available) before use."],"exampleFix":"// before\nUCrop.of(sourceUri, null).start(activity);\n// after\nUri destUri = Uri.fromFile(new File(getCacheDir(), \"cropped.jpg\"));\nUCrop.of(sourceUri, destUri).start(activity);","handlingStrategy":"validation","validationCode":"if (destUri == null) {\n    destUri = Uri.fromFile(new File(context.getCacheDir(), \"ucrop_result_\" + System.currentTimeMillis() + \".jpg\"));\n}","typeGuard":"boolean hasDestination(Uri src, Uri dest) { return src != null && dest != null; }","tryCatchPattern":"try {\n    UCrop.of(sourceUri, destUri).start(activity);\n} catch (NullPointerException e) {\n    Log.e(TAG, \"Output Uri missing\", e);\n}","preventionTips":["Never pass a possibly-null destination Uri; build a default in cache dir.","Compute the output path synchronously before launching the crop.","If the destination comes from getExternalFilesDir, handle its null return for unavailable storage.","Always pass the result through UCrop's result extras and persist the output Uri you provided."],"tags":["android","ucrop","null-pointer","uri","bitmap-load"],"backgroundTag":"null-argument","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}