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

标签 android uri android-mediaplayer android-5.0-lollipop

我目前正在尝试制作音频媒体播放器。我目前正在运行 Lollipop 。我在为媒体播放器设置数据源时遇到问题。首先,这是我设置数据源的方式:

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 player
E/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 的数据源?谢谢。

最佳答案

Lollipop 决定学习另一门类(class),从系统中获取文件。 (有人说是KitKat的,我在KitKat上还没遇到)。下面的代码是获取 Lollipop 上的文件路径

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

是媒体文档:

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

获取数据列:

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 转换为 Lollipop 上的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27879248/

相关文章:

android - 始终显示 PreferenceScreen 的滚动条

php - android 使用 json 与 php mysql 连接

java - 我的 ScrollView 不会滚动

java - 在android中将整数数组列表添加到数组列表中

java - 全局 MediaPlayer 声明不起作用

Android MediaPlayer.stop() 暂停而不是停止

android - 在android中同时播放图像和音频

webview - 如何在 Flutter 项目中指定 dart Uri 的编码

android - 如何通过编码将图像和.txt(多个文件)文件附加到gmail?

java - 将 SoundPool 与 URI 一起使用?