我正在使用FileProvider模式来为文件创建content:// uri,
FileProvider.getUriForFile(this, "com.myapp.provider", file)
Run Code Online (Sandbox Code Playgroud)
功能。我有清单,provider_paths和所有设置的标准方式,它创建了一个uri content://com.myapp.provider/external_files/music/mysong.mp3。
我的问题是,如果我尝试在另一个应用程序中获取真实文件路径,则由于该_data列不存在而无法正常工作(具体来说,日志中的错误是E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 0 columns.)。为了获取真实路径,我使用了非常标准的功能
final String column = MediaStore.Files.FileColumns.DATA;
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
Run Code Online (Sandbox Code Playgroud)
如果我使用其他应用程序共享同一文件,则会生成一个类似uri的uri content://com.otherapp.provider/external_files/music/mysong.mp3,我已经可以从中检索真实的文件路径。有什么想法可以做,以确保我的应用正确地将给定的uri插入到ContentResolver中?contentResolver.insert(...)不允许使用手动功能。我尝试了不同版本的provider_paths.xml,并为给定的uri授予了所有可能的读/写权限,但是我永远无法检索真实路径。
由我生成的uri本身可以正常工作,因为我可以读取文件或播放歌曲,我的问题是我无法检索所需的真实文件路径。 …