我们正在尝试获取保存在 SD 卡中的文件的路径,以传递给实用程序。然而,当我们使用 URI 打开游标时,只有两列被提取,路径(_data 列)丢失了。请让我知道如何获取路径。该查询适用于内部存储中的任何文件。
方案是“内容”。
代码 fragment
cursor = context.getContentResolver().query(uri, null, null, null, null);
if (null != cursor && cursor.moveToFirst())
{
int testcolCount = cursor.getColumnCount();
for (int i = 0; i < testcolCount; i++)
{
String colName = cursor.getColumnName(i);
String colValue = cursor.getString(i);
System.out.println("" + i + ": " + colName + ":" + colValue + "\n");
}
//This prints only two columns, _display_name and _size
}
最佳答案
根本不要尝试获取路径,Android 10 及更高版本的应用程序私有(private)目录之外的文件路径毫无用处,因为您无权访问它们。
参见 https://developer.android.com/training/data-storage#scoped-storage
使用 contentResolver
获取 FileDescriptor
以传递给 Utility
注释来自 https://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#DATA
This constant was deprecated in API level 29. Apps may not have filesystem permissions to directly access this path. Instead of trying to open this path directly, apps should use ContentResolver#openFileDescriptor(Uri, String) to gain access.
关于Android:从SD卡中文件的Uri获取文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59133506/