编程创建文件夹后Media Store失效,视频查询Cursor计数为0求解决方案
Hey there! Let's work through why your Cursor is returning a 0 count when trying to access videos from a folder you created programmatically. I’ve dealt with similar media store quirks before, so here are actionable fixes to try out:
Trigger a Media Library Scan
Android’s media store doesn’t automatically detect new folders or files immediately—you need to tell it to index your content. Here’s how to handle this across different Android versions:// For Android < 10: Use MediaScannerConnection MediaScannerConnection.scanFile(context, new String[]{yourFolderAbsolutePath}, new String[]{"video/*"}, (path, uri) -> { // Once scanning finishes, retry your Cursor query }); // For Android 10+: Use MediaStore API (scoped storage compliant) ContentValues values = new ContentValues(); values.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/YourCustomFolder"); // Match your folder's relative path values.put(MediaStore.Video.Media.IS_PENDING, 1); Uri contentUri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); // After saving your video file to this URI, mark it as non-pending values.clear(); values.put(MediaStore.Video.Media.IS_PENDING, 0); context.getContentResolver().update(contentUri, values, null, null);Note: The media store won’t index empty folders—make sure you’ve actually saved video files to the folder first.
Validate Permissions
Permission requirements vary by Android version, and missing them can block access to media:- Android 12 and below: Ensure you’ve requested
READ_EXTERNAL_STORAGE(andWRITE_EXTERNAL_STORAGEfor saving files) at runtime, not just declared them in the manifest. - Android 13+: Use
READ_MEDIA_VIDEOinstead of the old storage permissions—this is mandatory for targeting API 33+. - For Android 10+, avoid direct file path access where possible. Stick to the MediaStore API or Storage Access Framework (SAF) to comply with scoped storage rules.
- Android 12 and below: Ensure you’ve requested
Fix Your Cursor Query
Double-check that your query is targeting the right folder using reliable columns (avoid hardcoding absolute paths, which vary between devices):String selection = MediaStore.Video.Media.RELATIVE_PATH + " LIKE ?"; String[] selectionArgs = new String[]{"%YourCustomFolder%"}; // Match your folder name Cursor cursor = getContentResolver().query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, // Fetch all columns, or specify only what you need selection, selectionArgs, null );If you’re using absolute paths in your query, replace them with
RELATIVE_PATHfor better cross-device compatibility.Confirm Folder Creation Success
Make sure your folder was actually created without errors. Add a check after your folder creation code:File customFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "YourCustomFolder"); if (!customFolder.exists()) { boolean isCreated = customFolder.mkdirs(); if (!isCreated) { Log.e("FolderSetup", "Failed to create custom folder—check permissions or storage availability"); return; } }Also, avoid creating folders in restricted locations. Stick to public directories like
DIRECTORY_MOVIESorDIRECTORY_DCIMto avoid scoped storage blocks.Account for Version-Specific Behavior
Scoped storage changes introduced in Android 10, 11, and 13 often cause device-specific issues:- Android 10: Scoped storage is enabled by default, but you can use
android:requestLegacyExternalStorage="true"in your manifest as a temporary workaround (not recommended for new apps). - Android 11+: Legacy storage is deprecated, so you must use MediaStore or SAF for all media operations.
- Android 10: Scoped storage is enabled by default, but you can use
If none of these steps resolve the issue, sharing your exact folder creation code and Cursor query code would help narrow down the problem further!
内容的提问来源于stack exchange,提问作者Sagar H

