{"record":{"id":"f15b9c4101b7e746","repo":"Yalantis/uCrop","slug":"inputstream-for-given-input-uri-is-null","errorCode":null,"errorMessage":"InputStream for given input Uri is null","messagePattern":"InputStream for given input Uri is null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java","lineNumber":182,"sourceCode":"            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;\n            while ((length = inputStream.read(buffer)) > 0) {\n                outputStream.write(buffer, 0, length);\n            }\n        } finally {\n            BitmapLoadUtils.close(outputStream);\n            BitmapLoadUtils.close(inputStream);\n\n            // swap uris, because input image was copied to the output destination","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java#L164-L200","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nUCrop.of(pickedUri, destUri).start(this); // pickedUri granted only temporarily\n// after\nfinal int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;\ngetContentResolver().takePersistableUriPermission(pickedUri, flags);\nUCrop.of(pickedUri, destUri).start(this);","handlingStrategy":"try-catch","validationCode":"boolean isReadable(Uri uri) {\n    try (InputStream in = getContentResolver().openInputStream(uri)) {\n        return in != null;\n    } catch (Exception e) { return false; }\n}","typeGuard":"boolean isReadableUri(android.net.Uri uri) {\n    return uri != null && (\"file\".equals(uri.getScheme())\n        ? new java.io.File(uri.getPath()).canRead()\n        : isReadable(uri));\n}","tryCatchPattern":"if (!isReadableUri(pickedUri)) {\n    Toast.makeText(this, \"Image no longer available, please pick again\", Toast.LENGTH_LONG).show();\n    return;\n}\nUCrop.of(pickedUri, destUri).start(this);","preventionTips":["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."],"tags":["android","ucrop","contentresolver","input-stream","uri"],"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"}