{"record":{"id":"86581d489575bf66","repo":"Tencent/QMUI_Android","slug":"failed-to-create-new-mediastore-record","errorCode":null,"errorMessage":"Failed to create new MediaStore record.","messagePattern":"Failed to create new MediaStore record\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"photo/src/main/java/com/qmuiteam/photo/util/QMUIPhotoHelper.kt","lineNumber":67,"sourceCode":"        writer: (OutputStream) -> Unit\n    ): Uri? {\n        val contentValues = ContentValues().apply {\n            put(MediaStore.MediaColumns.DISPLAY_NAME, name)\n            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 {","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/Tencent/QMUI_Android/blob/026e7d486677d6593a96689cd590ec3561176717/photo/src/main/java/com/qmuiteam/photo/util/QMUIPhotoHelper.kt#L49-L85","documentation":"saveToStore inserts a new MediaStore.Images record via ContentResolver.insert(). When the MediaStore provider returns null (insert rejected), an IOException(\"Failed to create new MediaStore record.\") is thrown because no target Uri exists to write the image into.","triggerScenarios":"contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) returning null — typically missing WRITE_EXTERNAL_STORAGE (pre-Q) or volume unavailable/readonly.","commonSituations":"Saving a photo without storage permission on Android 9 or lower; external storage mounted as read-only (USB/SD emulation); device storage full or provider crash.","solutions":["Request READ/WRITE_EXTERNAL_STORAGE runtime permission on API <= 28 before saving.","Verify external storage state via Environment.getExternalStorageState() == MEDIA_MOUNTED.","Check free disk space and retry when storage is available.","Catch the IOException and surface a user-facing save failure."],"exampleFix":"// before\nQMUIPhotoHelper.saveToStore(context, ...) // no permission requested\n// after\nrequestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQ) { QMUIPhotoHelper.saveToStore(context, ...) }","handlingStrategy":"try-catch","validationCode":"if (Build.VERSION.SDK_INT < 29 &&\n  ContextCompat.checkSelfPermission(context, WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) return\nif (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) return","typeGuard":null,"tryCatchPattern":"try { saveToStore(context, contentValues, writer) } catch (e: IOException) { showSaveFailedToast(e) }","preventionTips":["Request storage permissions on API <= 28 before saving","Check Environment.getExternalStorageState() == MEDIA_MOUNTED","Monitor free disk space"],"tags":["android","mediastore","storage","io","permissions"],"backgroundTag":"permission-denied","analyzedSha":"026e7d486677d6593a96689cd590ec3561176717","analyzedAt":"2026-09-06T13:32:24.816Z","contentChangedAt":"2026-09-06T13:32:24.816Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}