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 destinationView on GitHub (pinned to f788b534b4)
Solutions
- Persist and re-take permissions: call takePersistableUriPermission on the Uri after receiving it (when flags allow).
- Re-pick the image with the Activity Result API instead of reusing a stale Uri across app restarts.
- Verify the Uri is readable before cropping: contentResolver.openInputStream(uri) != null, and close the probe stream.
- Copy the source to app-local storage first, then pass the local file Uri to uCrop.
- 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
- Take persistable URI permissions for picked content URIs when flags allow.
- Re-pick images rather than reusing Uris across process restarts.
- Prefer copying picked images into app storage before cropping.
- Test the crop flow after the granting activity's process has died.
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
- Invalid Uri scheme%s
- Output Uri is null - cannot copy image
- Output Uri is null - cannot download image
- Index [selectedByDefault = %d] (0-based) cannot be higher or
- %s must implement UCropFragmentCallback
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/f15b9c4101b7e746.
Report an issue: GitHub.