Yalantis/uCrop · error · NullPointerException

InputStream for given input Uri is null

Error message

InputStream for given input Uri is null

What it means

In BitmapLoadTask.copyFile, ContentResolver.openInputStream(inputUri) returned null, meaning the system resolver recognized the content URI but could not provide a stream for it. The task converts that into a NullPointerException with this message since no data can be read to copy.

Source

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

            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;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
        } finally {
            BitmapLoadUtils.close(outputStream);
            BitmapLoadUtils.close(inputStream);

            // swap uris, because input image was copied to the output destination

View on GitHub (pinned to f788b534b4)

Solutions

  1. Persist and re-take permissions: call takePersistableUriPermission on the Uri after receiving it (when flags allow).
  2. Re-pick the image with the Activity Result API instead of reusing a stale Uri across app restarts.
  3. Verify the Uri is readable before cropping: contentResolver.openInputStream(uri) != null, and close the probe stream.
  4. Copy the source to app-local storage first, then pass the local file Uri to uCrop.
  5. Wrap the crop launch in try-catch so a failed load surfaces as a handled result instead of a crash.

Example fix

// before
UCrop.of(pickedUri, destUri).start(this); // pickedUri granted only temporarily
// after
final int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
getContentResolver().takePersistableUriPermission(pickedUri, flags);
UCrop.of(pickedUri, destUri).start(this);
Defensive patterns

Strategy: try-catch

Validate before calling

boolean isReadable(Uri uri) {
    try (InputStream in = getContentResolver().openInputStream(uri)) {
        return in != null;
    } catch (Exception e) { return false; }
}

Type guard

boolean isReadableUri(android.net.Uri uri) {
    return uri != null && ("file".equals(uri.getScheme())
        ? new java.io.File(uri.getPath()).canRead()
        : isReadable(uri));
}

Try / catch

if (!isReadableUri(pickedUri)) {
    Toast.makeText(this, "Image no longer available, please pick again", Toast.LENGTH_LONG).show();
    return;
}
UCrop.of(pickedUri, destUri).start(this);

Prevention

When it happens

Trigger: Opening a content:// Uri that the provider cannot serve — the provider returned null from openInputStream, commonly for revoked permissions, temporary grant URIs from another app after process restart, or cloud/non-local documents.

Common situations: Receiving ACTION_GET/ACTION_PICK results and processing them after the granting activity's process died; using a TAKE_PICTURE preview URI instead of the final saved file URI; Google Drive / cloud 'document' URIs requiring the documents provider to be consulted first.

Related errors


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