English 中文(简体)
和机器人 - 光标没有找到数据列(_D)
原标题:android - cursor didn t have _data column not found
  • 时间:2015-09-18 21:21:58
  •  标签:
  • android
When run below code to query a file on sdcard, I always get a null. 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; } The media file test.mp3 has been pushed to sdcard and I can find it from the database. Then from the cursor dump, I found it actually didn t contain _data field. 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 : <<<<< I am using android L. But database really has _data field. why I can not query it by resolver?
问题回答
Finally, I have understood it caused by different Provider. we need to add logic to handle different provider case. Below code snippet works for me (refer to 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; }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签