当在代码下运行以查询SD卡上的文件时,我总是得到一个空值。
public String getRealPathFromURI(Context context, Uri uri)
{
String fileName="unknown";
if (uri.getScheme().toString().compareTo("content") == 0)
{
Cursor cursor = context.getContentResolver().query(uri,null,null,null,null);
if (cursor.moveToFirst())
{
Log.e(TAG, "dump cursor:" + DatabaseUtils.dumpCursorToString(cursor));
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
String name = cursor.getString(column_index);
if (name != null) {
uri = Uri.parse(cursor.getString(column_index));
fileName = uri.getLastPathSegment().toString();
}
}
}else if (uri.getScheme().compareTo("file") == 0){
fileName = uri.getLastPathSegment().toString();
}else {
fileName = fileName + "_" + uri.getLastPathSegment();
}
Log.e(TAG,"fileName:" + fileName);
return fileName;
}
媒体文件test.mp3已被推送到SD卡,我可以从数据库中找到它。
然后从光标转储中,我发现它实际上并没有包含_data字段。
11138 09-18 16:14:53.881 27848 27848 E MyExam : dump cursor:>>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@9ab47b6
11139 09-18 16:14:53.881 27848 27848 E MyExam : 0 {
11140 09-18 16:14:53.881 27848 27848 E MyExam : document_id=primary:test.mp3
11141 09-18 16:14:53.881 27848 27848 E MyExam : mime_type=audio/mpeg
11142 09-18 16:14:53.881 27848 27848 E MyExam : _display_name=test.mp3
11143 09-18 16:14:53.881 27848 27848 E MyExam : last_modified=1441221715000
11144 09-18 16:14:53.881 27848 27848 E MyExam : flags=70
11145 09-18 16:14:53.881 27848 27848 E MyExam : _size=14400116
11146 09-18 16:14:53.881 27848 27848 E MyExam : }
11147 09-18 16:14:53.881 27848 27848 E MyExam : <<<<<
我正在使用android L。
但是数据库确实有_data字段。为什么我不能通过解析器查询它?
答案 0 :(得分:0)
最后,我理解它是由不同的提供商造成的。我们需要添加逻辑来处理不同的提供者案例。
以下代码段适用于我(请参阅this post):
public String getPathFromUri(final Context context, final Uri uri) {
boolean isAfterKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
Log.e(TAG,"uri:" + uri.getAuthority());
if (isAfterKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if ("com.android.externalstorage.documents".equals(
uri.getAuthority())) {// ExternalStorageProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}else {
return "/stroage/" + type + "/" + split[1];
}
}else if ("com.android.providers.downloads.documents".equals(
uri.getAuthority())) {// DownloadsProvider
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}else if ("com.android.providers.media.documents".equals(
uri.getAuthority())) {// MediaProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
contentUri = MediaStore.Files.getContentUri("external");
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}else if ("content".equalsIgnoreCase(uri.getScheme())) {//MediaStore
return getDataColumn(context, uri, null, null);
}else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String[] projection = {
MediaStore.Files.FileColumns.DATA
};
try {
cursor = context.getContentResolver().query(
uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int cindex = cursor.getColumnIndexOrThrow(projection[0]);
return cursor.getString(cindex);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}