{"record":{"id":"fa4eeb814ad4689f","repo":"Tencent/QMUI_Android","slug":"failed-to-get-output-stream","errorCode":null,"errorMessage":"Failed to get output stream.","messagePattern":"Failed to get output stream\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"photo/src/main/java/com/qmuiteam/photo/util/QMUIPhotoHelper.kt","lineNumber":71,"sourceCode":"            put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis())\n            put(MediaStore.MediaColumns.MIME_TYPE, mimeType)\n            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {\n                put(MediaStore.MediaColumns.RELATIVE_PATH, dirName)\n                put(MediaStore.MediaColumns.IS_PENDING, 1)\n            }\n        }\n\n        val contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI\n        var stream: OutputStream? = null\n        var uri: Uri? = null\n        try {\n            uri = context.contentResolver.insert(contentUri, contentValues)\n            if (uri == null) {\n                throw IOException(\"Failed to create new MediaStore record.\")\n            }\n            stream = context.contentResolver.openOutputStream(uri)\n            if (stream == null) {\n                throw IOException(\"Failed to get output stream.\")\n            }\n            writer.invoke(stream)\n            contentValues.clear()\n            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {\n                contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0)\n                context.contentResolver.update(uri, contentValues, null, null)\n            }\n            return uri\n        } catch (e: Throwable) {\n            Log.i(TAG, \"saveToStore failed.\", e)\n            if (uri != null) {\n                context.contentResolver.delete(uri, null, null)\n            }\n        } finally {\n            stream?.close()\n        }\n        return null\n    }","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/Tencent/QMUI_Android/blob/026e7d486677d6593a96689cd590ec3561176717/photo/src/main/java/com/qmuiteam/photo/util/QMUIPhotoHelper.kt#L53-L89","documentation":"After the MediaStore record is created, saveToStore calls contentResolver.openOutputStream(uri). If the provider refuses to hand out an output stream (null), an IOException(\"Failed to get output stream.\") is thrown so the write aborts cleanly and pending cleanup runs.","triggerScenarios":"openOutputStream(uri) returning null even though insert succeeded — provider locked the file, IS_PENDING conflict, storage suddenly unavailable, or security exception surfaced as null.","commonSituations":"Concurrent modification of the MediaStore entry; low storage making the file unopenable; OEM provider quirks; passing a uri that was invalidated after insert.","solutions":["Retry the save operation; transient provider failures are common.","Check free storage space before writing.","Verify the returned Uri is still valid (contentResolver.getType(uri) != null) before opening the stream.","Catch the IOException and delete the orphan pending record if needed."],"exampleFix":"// before\nstream = context.contentResolver.openOutputStream(uri) ?: throw IOException(...)\n// after\ntry { saveToStore(...) } catch (e: IOException) { cleanupPendingUri(uri); retryLater() }","handlingStrategy":"retry","validationCode":"val stream = context.contentResolver.openOutputStream(uri)\nif (stream == null) { /* schedule retry or cleanup pending record */ }","typeGuard":null,"tryCatchPattern":"var attempt = 0\nwhile (attempt < 3) {\n  try { saveToStore(...); break } catch (e: IOException) { if (++attempt == 3) report(e) }\n}","preventionTips":["Retry transient ContentResolver failures","Delete orphan IS_PENDING records on failure","Verify uri validity with contentResolver.getType(uri) before writing"],"tags":["android","mediastore","storage","io","outputstream"],"backgroundTag":"file-open-failed","analyzedSha":"026e7d486677d6593a96689cd590ec3561176717","analyzedAt":"2026-09-06T13:32:24.816Z","contentChangedAt":"2026-09-06T13:32:24.816Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}