Android使用MediaStore创建文件后卸载重装无法读取的问题求助
问题:MediaStore创建的文件卸载重装后无法检索
我通过MediaStore创建了文件并写入数据,但卸载应用后重新安装,无法检索到该文件及其数据。执行读取操作仅弹出提示“No file found in "" + Environment.DIRECTORY_DOCUMENTS + "/Sooraj/"”的Toast。
以下是实现文件创建与读取的代码:
private void createFile() { byte[] data = "test".getBytes(); ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DISPLAY_NAME, "customstore"); //file name values.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain"); //file extension, will automatically add to file values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS + "/Sooraj/"); //end "/" is not mandatory Uri uri = getContentResolver().insert(MediaStore.Files.getContentUri("external"), values); //important! text2.setText("Created test"); try { OutputStream outputStream = getContentResolver().openOutputStream(uri); if (outputStream != null) { outputStream.write(data); outputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } private void readFile() { Uri contentUri = MediaStore.Files.getContentUri("external"); String selection = MediaStore.MediaColumns.RELATIVE_PATH + "=?"; String[] projection = new String[]{"_id"}; String[] selectionArgs = new String[]{Environment.DIRECTORY_DOCUMENTS + "/Sooraj/"}; Cursor cursor = getContentResolver().query(contentUri, null, selection, selectionArgs, null); Uri uri = null; if (cursor.getCount() == 0) { Toast.makeText(MainActivity.this, "No file found in \"" + Environment.DIRECTORY_DOCUMENTS + "/Sooraj/\"", Toast.LENGTH_LONG).show(); } else { while (cursor.moveToNext()) { @SuppressLint("Range") String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)); if (fileName.equals("customstore.txt")) { @SuppressLint("Range") long id = cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns._ID)); uri = ContentUris.withAppendedId(contentUri, id); break; } } if (uri == null) { Toast.makeText(MainActivity.this, "\"customstore.txt\" not found", Toast.LENGTH_SHORT).show(); } else { try { InputStream inputStream = getContentResolver().openInputStream(uri); int size = inputStream.available(); byte[] bytes = new byte[size]; inputStream.read(bytes); inputStream.close(); String jsonString = new String(bytes, StandardCharsets.UTF_8); text3.setText(jsonString); } catch (IOException e) { Toast.makeText(MainActivity.this, "Fail to read file", Toast.LENGTH_SHORT).show(); } } } }
解决方案
1. 配置读取权限
Android 10及以上版本中,读取其他应用创建的公共存储文件需要READ_EXTERNAL_STORAGE权限:
- 在
AndroidManifest.xml中声明权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- 在运行时动态请求该权限(危险权限需用户手动授权)。
2. 修正路径查询逻辑
MediaStore存储的RELATIVE_PATH格式可能与你构造的字符串不一致(比如是否带末尾斜杠),精确匹配容易失败,可改为:
- 模糊匹配路径下的所有文件:
String selection = MediaStore.MediaColumns.RELATIVE_PATH + " LIKE ?"; String[] selectionArgs = new String[]{Environment.DIRECTORY_DOCUMENTS + "/Sooraj/%"};
- 或直接通过文件名查询,跳过路径匹配:
String selection = MediaStore.MediaColumns.DISPLAY_NAME + "=?"; String[] selectionArgs = new String[]{"customstore.txt"};
3. 明确完整文件名
创建文件时直接设置带后缀的文件名,避免系统自动添加后缀时出现不一致:
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "customstore.txt"); values.put(MediaStore.MediaColumns.MIME_TYPE, "text/plain");
4. 优化查询投影
明确指定需要查询的列,避免潜在的列缺失问题:
String[] projection = new String[]{ MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DISPLAY_NAME };
验证步骤
- 打开系统文件管理器,检查
Documents/Sooraj目录下是否存在customstore.txt; - 确认应用已获得
READ_EXTERNAL_STORAGE权限; - 逐步修改代码,测试查询逻辑是否生效。
内容的提问来源于stack exchange,提问作者Suraj nayi
相关产品推荐
相关产品推荐

