Yalantis/uCrop · error · NullPointerException

Output Uri is null - cannot copy image

Error message

Output Uri is null - cannot copy image

What it means

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.

Source

Thrown at ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java:174

        } else if (isContentUri(mInputUri)) {
            try {
                copyFile(mInputUri, mOutputUri);
            } catch (NullPointerException | IOException e) {
                Log.e(TAG, "Copying failed", e);
                throw e;
            }
        } else if (!isFileUri(mInputUri)) {
            String inputUriScheme = mInputUri.getScheme();
            Log.e(TAG, "Invalid Uri scheme " + inputUriScheme);
            throw new IllegalArgumentException("Invalid Uri scheme" + inputUriScheme);
        }
    }

    private void copyFile(@NonNull Uri inputUri, @Nullable Uri outputUri) throws NullPointerException, IOException {
        Log.d(TAG, "copyFile");

        if (outputUri == null) {
            throw new NullPointerException("Output Uri is null - cannot copy image");
        }

        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = mContext.getContentResolver().openInputStream(inputUri);
            if (inputStream == null) {
                throw new NullPointerException("InputStream for given input Uri is null");
            }

            if (isContentUri(outputUri)) {
                outputStream = mContext.getContentResolver().openOutputStream(outputUri);
            } else {
                outputStream = new FileOutputStream(new File(outputUri.getPath()));
            }

            byte buffer[] = new byte[1024];
            int length;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Always pass a non-null destination Uri to UCrop.of(), e.g. Uri.fromFile(new File(getCacheDir(), "cropped.jpg")).
  2. Ensure the out-uri extra (UCrop.EXTRA_OUTPUT_URI) is actually populated when constructing options programmatically.
  3. Null-check the destination before launching the crop intent and bail early with user feedback.
  4. If the path comes from getExternalFilesDir or similar, handle its null return (storage not available) before use.

Example fix

// before
UCrop.of(sourceUri, null).start(activity);
// after
Uri destUri = Uri.fromFile(new File(getCacheDir(), "cropped.jpg"));
UCrop.of(sourceUri, destUri).start(activity);
Defensive patterns

Strategy: validation

Validate before calling

if (destUri == null) {
    destUri = Uri.fromFile(new File(context.getCacheDir(), "ucrop_result_" + System.currentTimeMillis() + ".jpg"));
}

Type guard

boolean hasDestination(Uri src, Uri dest) { return src != null && dest != null; }

Try / catch

try {
    UCrop.of(sourceUri, destUri).start(activity);
} catch (NullPointerException e) {
    Log.e(TAG, "Output Uri missing", e);
}

Prevention

When it happens

Trigger: 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).

Common situations: 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).

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/cdc46d939eb19d55. Report an issue: GitHub.