Android 10 – 使用不同的Uri类型查询MediaStore

huangapple 未分类评论46阅读模式
英文:

Android 10 - Query MediaStore with different Uri types

问题

我遇到了Android标准选择器的问题。我有以下代码用于使用标准选择器(通过ACTION_GET_CONTENT调用)查询mediastore的URI:

private AudioRecord getAudioInfo(Uri uri) {
    AudioRecord rec = new AudioRecord();
    try {
        if (DocumentsContract.isDocumentUri(this, uri)){
            String[] data = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM};
            String wholeID = DocumentsContract.getDocumentId(uri);
            String id = wholeID.split(":")[1];
            String sel = MediaStore.Images.Media._ID + "=?";
            CursorLoader loader = new CursorLoader(getApplicationContext(),
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, data, sel, new String[]{ id }, null);
            Cursor cursor = loader.loadInBackground();
            cursor.moveToFirst();
            rec.setData(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
            rec.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
            rec.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
            rec.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
            rec.setFilename("8D " + rec.getTitle() + ".mp3");
            cursor.close();
            return rec;
        } else {
            String[] data = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM};
            CursorLoader loader = new CursorLoader(getApplicationContext(), uri, data, null, null, null);
            Cursor cursor = loader.loadInBackground();
            cursor.moveToFirst();
            rec.setData(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
            rec.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
            rec.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
            rec.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
            rec.setFilename("8D " + rec.getTitle() + ".mp3");
            cursor.close();
            return rec;
        }

    } catch(Exception e){
        e.printStackTrace();
        return rec;
    }
}

选择器中有不同的选项可以选择文件。这些选项在下面的图像中显示并编号:

(图像已省略)

以下是不同编号的输出URI:

  1. (Musik):

content://media/external/audio/media/3301

  1. (Downloads):

content://com.android.providers.downloads.documents/document/msf%3A3301

  1. (VOG-L29):

content://com.android.externalstorage.documents/document/primary%3ADownload%2FJuice%20WRLD%20Unreleased%2FBag.mp3

  1. (Dateien):

content://media/external/audio/media/3301

  1. (Audio):

content://com.android.providers.media.documents/document/audio%3A3301

除了编号为3的URI外,该代码适用于所有URI。在编号为3的URI中,没有任何ID可用于查询mediastore。

因此,问题是:

我如何使用这样的URI查询mediastore以获取有关艺术家等更多信息?

英文:

I have a problem with the android standard picker.
I have the following code to query the mediastore with a uri from the standard picker(call with ACTION_GET_CONTENT):

        private AudioRecord getAudioInfo(Uri uri) {
        AudioRecord rec = new AudioRecord();
        try {
            if (DocumentsContract.isDocumentUri(this, uri)){
                String[] data = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM};
                String wholeID = DocumentsContract.getDocumentId(uri);
                String id = wholeID.split(":")[1];
                String sel = MediaStore.Images.Media._ID + "=?";
                CursorLoader loader = new CursorLoader(getApplicationContext(),
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, data, sel, new String[]{ id }, null);
                Cursor cursor = loader.loadInBackground();
                cursor.moveToFirst();
                rec.setData(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                rec.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                rec.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                rec.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
                rec.setFilename("8D " + rec.getTitle() + ".mp3");
                cursor.close();
                return rec;
            } else {
                String[] data = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM};
                CursorLoader loader = new CursorLoader(getApplicationContext(), uri, data, null, null, null);
                Cursor cursor = loader.loadInBackground();
                cursor.moveToFirst();
                rec.setData(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                rec.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                rec.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                rec.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
                rec.setFilename("8D " + rec.getTitle() + ".mp3");
                cursor.close();
                return rec;
            }

        }catch(Exception e){
            e.printStackTrace();
            return rec;
        }
    }

-

In the picker i have different options to pick a file. These options are shown and numbered in the image below:

Android 10 – 使用不同的Uri类型查询MediaStore

Here are the output Uri's for the different numbers:

  1. (Musik:
    >content://media/external/audio/media/3301

  2. (Downloads):
    >content://com.android.providers.downloads.documents/document/msf%3A3301

  3. (VOG-L29):
    >content://com.android.externalstorage.documents/document/primary%3ADownload%2FJuice%20WRLD%20Unreleased%2FBag.mp3

  4. (Dateien):
    >content://media/external/audio/media/3301

  5. (Audio):
    >content://com.android.providers.media.documents/document/audio%3A3301

The code works for all Uri's except number 3. In this Uri there isn't any ID which i can use to query the mediastore.

So the question is:

How can i use such a Uri to query the mediastore for more informations about artist and so on?

huangapple
  • 本文由 发表于 2020年5月4日 20:48:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61592584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定