Android-将URI转换为棒棒糖上的文件路径

Android-将URI转换为棒棒糖上的文件路径,第1张

概述我目前正在尝试制作媒体播放器来播放音频.我目前正在运行棒棒糖.我遇到了为媒体播放器设置dataSource的问题.首先,这是我设置dataSource的方法:publicvoidplaySong(){player.reset();SongselSong=songs.get(songPos);longcurrSong=selSong.getId();/

我目前正在尝试制作媒体播放器来播放音频.我目前正在运行棒棒糖.我遇到了为媒体播放器设置dataSource的问题.首先,这是我设置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();}

而Uri得出以下结论:

content://media/external/audio/media/22

我做了一些研究,据我了解,在AndroID 4.1之后,您将无法再将URI用于数据源.当我使用上面的代码运行该应用程序时,会出现以下错误:

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

因此,现在我需要将URI转换为文件路径,并将其作为数据源提供.而且,在进行了更多研究之后,kitkat似乎改变了提供URI的方式,因此很难从URI中获取文件路径.但是,我不确定此更改是否会保留到AndroID Lollipop 5.0.2中.

本质上,我具有歌曲的URI,但是我需要向数据源提供URI以外的其他内容.有什么方法可以在Lollipop上转换URI,如果没有,我如何只知道歌曲的ID才能提供数据源?谢谢.

解决方法:

棒棒糖决定采取另一门课程,从系统中获取文件. (有人说它是来自KitKat,但我还没有在KitKat上遇到过).下面的代码是获取棒棒糖上的文件路径

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

如果仍然有问题,那么this是检查图像,音频,视频,文件等的完整答案.

总结

以上是内存溢出为你收集整理的Android-将URI转换为棒棒糖上的文件路径全部内容,希望文章能够帮你解决Android-将URI转换为棒棒糖上的文件路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存