32385

Get the path from Uri in Android Kitkat throws Illegal Argument exception

I'm trying to get the path from uri. it works in devices below kitkat. I googled and found that I need to get the document Id and then query it with the gallery items. And it works. But I'm facing problem with videos. When i select the video from Videos section, it works. But When I select it from Gallery, my app crashes, giving Illegal Argument exception- Not a document type. I know why it's happening. It is because I'm looking for a document type of video in Gallery section. But how to prevent this. So that I can select videos from both Gallery & Videos Section.

Here is my code:

public static String getRealPathFromURI(Context context, Uri contentUri,String type) { if(isKitkatBelow()) { String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }else{ String docId= DocumentsContract.getDocumentId(contentUri); String id= docId.split(":")[1]; if(type== TYPE_IMAGE){ String[] proj = { MediaStore.Images.Media.DATA }; String whereClause=MediaStore.Images.Media._ID + "=?"; Cursor cursor=context.getContentResolver().query(getUri(type),proj,whereClause,new String[]{id},null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }else{ String[] proj = { MediaStore.Video.Media.DATA }; String whereClause=MediaStore.Video.Media._ID + "=?"; Cursor cursor=context.getContentResolver().query(getUri(type),proj,whereClause,new String[]{id},null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } }

This is happening because I'm trying to search a file of type MediaStore.Video.Media.DATA in Gallery. But for devices below kitkat, MediaStore.Images.Media.DATA gets the video uri, from the gallery too without causing any crash. Thanks in Advance..

Answer1:

You can use for getting absolute path from the Uri of selected file because of differences in both KitKat(API 19) and below Kitkat in android to achieve this using following code,

public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { 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]; } // TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) { 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); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context * The context. * @param uri * The Uri to query. * @param selection * (Optional) Filter used in the query. * @param selectionArgs * (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri * The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri .getAuthority()); } /** * @param uri * The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri .getAuthority()); } /** * @param uri * The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri .getAuthority()); }

Recommend

  • _block_type_is_valid phead- nblockuse on delete command
  • wxPython - wxGrid - How to detect which column moves where
  • Creating an excel worksheet VA code
  • Large Photo Version from contacts in android
  • How to find origID for getThumbnail when taking a picture with camera.TakePicture
  • Xamarin Android Request to Server is not establishing instead getting an exception?
  • Calling a Java method from a sqlite trigger (android)
  • Error casting Object[] to ContentValues[]
  • Clear chrome browser history programmatically
  • Get file path from Uri from Video Chooser
  • How to export MS Access table into a csv file in Python using e.g. pypyodbc
  • Morphia is there a difference between fetch and asList in performance wise
  • Can't set character_set_results to latin1
  • TelephonyManager crashing on android studio
  • psycopg2.ProgrammingError: syntax error at or near “stdin” error when trying to copy_from redshift
  • Tkinter: Determine Widget Position relative to Root Window
  • MIT Kerberos fails to locate TGT in MSLSA cache
  • Query timeout expired in django-mssql when executing custom SQL directly
  • Pre-populated SQLite Database not reading properly in Android Studio
  • Converting query results into DataFrame in python
  • Possible to get mouse events fired when cursor is outside page?
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • LevelDB C iterator
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?