Android - convert URI to file path on candy

I'm currently trying to make a media player for audio. I am currently using candy. I am having a problem setting up a dataSource for a media player. First, here is how I set the dataSource:

public void playSong() { player.reset(); Song selSong = songs.get(songPos); long currSong = selSong.getId(); //Get Uri of song Uri trackUri = ContentUris.withAppendedId( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, currSong); try { //Tell the player the song to play player.setDataSource(getApplicationContext(), trackUri); } catch (Exception e) { Log.e("MUSIC SERVICE", "Error setting data source", e); Log.d("URI Path", trackUri.toString()); } //Will prepare the song and call onPrepare of player player.prepareAsync(); } 

And Uri comes to this:

Contents: // media / external / audio / media / 22

I did some research, and in my opinion, after Android 4.1, you can no longer use the URI for the dataSource. When I run this application with the above code, I will get this error:

 E/MediaPlayer﹕ Unable to create media player E/MUSIC SERVICE﹕ Error setting data source java.io.IOException: setDataSource failed.: status=0x80000000 at android.media.MediaPlayer.nativeSetDataSource(Native Method) 

So now I need to convert the URI to the file path and provide this as a dataSource. And, after further research, kitkat seems to have changed the way the URI is provided, so it’s hard to get the file path from the URI. However, I'm not sure if this change has been preserved in Android Lollipop 5.0.2.

Essentially, I have a song URI, but I need to provide something other than a URI in a dataSource. Is there a way I can convert a URI to Lollipop, and if not, how can I provide a dataSource to only know the song id? Thank.

+3
android uri android-5.0-lollipop android-mediaplayer
Jan 10 '15 at 17:48
source share
1 answer

Lollipop decided to take another course with getting files from the system. (Some say this is from KitKat, but I haven't met him in KitKat yet). The code below is to get the path to the file for the candy

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; String filePath = getDataColumn(context, contentUri, selection, selectionArgs); } 

isMediaDocument:

 public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } 

getDataColumn:

 private 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; } 

If you still have problems, this is a complete answer that checks images, audio, video, files, etc.

0
Mar 20 '15 at 9:36
source share



All Articles