无法使用内容解析程序返回所有相册URI

时间:2015-11-17 07:57:13

标签: android

我正在制作音乐播放器,我注意到Play商店中的一些其他音乐播放器能够在我的手机上显示整套专辑封面,其中一些其他播放器,包括我自己的播放器,都是无法全部显示。

我能够设置媒体扫描程序以在给定的外部文件目录中注册音乐文件

File file = new File(Environment.getExternalStorageDirectory() + "/Music");
new SingleMediaScanner(this, file);

这是scanner类,它使用递归搜索整个/Music目录并单独注册文件

public class SingleMediaScanner implements MediaScannerConnectionClient {

private MediaScannerConnection mMs;
private File mFile;

public SingleMediaScanner(Context context, File f) {
    mFile = f;
    mMs = new MediaScannerConnection(context, this);
    mMs.connect();
}

@Override
public void onMediaScannerConnected() {

    listDirectory(mFile,1);
}

@Override
public void onScanCompleted(String path, Uri uri) {
    mMs.disconnect();
}

public void listDirectory(File dirPath, int level) {

    // See code below

}
}

这是带递归的listDirectory()方法

public void listDirectory(File dirPath, int level) {

        //File dir = new File(dirPath);

        File[] firstLevelFiles = dirPath.listFiles();
        if (firstLevelFiles != null && firstLevelFiles.length > 0) {
            for (File aFile : firstLevelFiles) {
                if (aFile.isDirectory()) {
                    listDirectory(aFile, level + 1);
                } else {
                    Log.v("TAG file name",aFile.getName()); // Appears to be working
                    mMs.scanFile(aFile.getAbsolutePath(), null);
                }
            }
        }
}

但奇怪的是,我可以在没有整个扫描仪类的情况下完成,结果也是一样的。当我为相册URI运行查询时,我仍然得到null以换取丢失的专辑封面URI。

private String GetAlbumArtURI(String[] albumID) {

        final Cursor mCursor = getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Audio.Albums.ALBUM_ART},
                MediaStore.Audio.Albums._ID + "=?",
                albumID,
                null
        );

        if (mCursor.moveToFirst()) {

            return mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
        }
        else {
            mCursor.close();
            return null;
        }
}

我正在让albumID对音乐元数据运行另一个查询。

修改

我打电话给GetAlbumArtURI

songObjectList = new ArrayList<>();
scanMedia();
Cursor songListCursor = GetSongListCursor();
MakeLists(songListCursor);

GetSongListCursor

private Cursor GetSongListCursor() {

    Uri contentURI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    String[] projection = {
    MediaStore.Audio.Media.ALBUM,
    MediaStore.Audio.Media.ARTIST,
    MediaStore.Audio.Media.TITLE,
    MediaStore.Audio.Media.DATA,
    MediaStore.Audio.Media.DURATION,
    MediaStore.Audio.Media.ALBUM_ID};

    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    String order = MediaStore.Audio.Media.TITLE + " ASC";

    final Cursor mCursor = getContentResolver().query(contentURI, projection, selection, null, order);

    return mCursor;
}

MakeLists()是我致电GetAlbumArtURI

的地方
private void MakeLists(Cursor mCursor) {

        try{

            if (mCursor.moveToFirst()) {

                do {

                    // Initialize songObject
                    SongObject songObject = new SongObject();

                    // Add albumURI
                    String[] albumID = {mCursor.getString(5)};
                    songObject.albumArtURI = GetAlbumArtURI(albumID);

                    // add albumTitle
                    String albumTitle = mCursor.getString(0);
                    songObject.albumTitle = albumTitle;

                    // Add artist or add "Unknown Artist"
                    String artist = mCursor.getString(1);
                    if (artist.equals("<unknown>")) {

                        artist = "Unknown Artist";
                        songObject.artist = artist;
                    }
                    else {

                        artist = mCursor.getString(1);
                        songObject.artist = artist;
                    }

                    String songTitle = mCursor.getString(2);
                    songObject.songTitle = songTitle;

                    String songPath = mCursor.getString(3);
                    songObject.songPath = songPath;

                    String songDuration = mCursor.getString(4);
                    songObject.songDuration = songDuration;

                    songObjectList.add(songObject);

                    String albumArtist = songObject.artist; 
                    String albumArtURI = songObject.albumArtURI;


                } while (mCursor.moveToNext());
            }

        } finally {

            mCursor.close();
        }
}

修改

专辑IDS

11-17 00:50:17.287  V/TAG ALBUM_ID: 16
11-17 00:50:17.297  V/TAG ALBUM_ID: 16
11-17 00:50:17.307  V/TAG ALBUM_ID: 25
11-17 00:50:17.327  V/TAG ALBUM_ID: 25
11-17 00:50:17.337  V/TAG ALBUM_ID: 25
11-17 00:50:17.347  V/TAG ALBUM_ID: 25
11-17 00:50:17.347  V/TAG ALBUM_ID: 24
11-17 00:50:17.357  V/TAG ALBUM_ID: 22
11-17 00:50:17.367  V/TAG ALBUM_ID: 22
11-17 00:50:17.367  V/TAG ALBUM_ID: 11
11-17 00:50:17.367  V/TAG ALBUM_ID: 11
11-17 00:50:17.377  V/TAG ALBUM_ID: 14
11-17 00:50:17.387  V/TAG ALBUM_ID: 11
11-17 00:50:17.417  V/TAG ALBUM_ID: 11
11-17 00:50:17.427  V/TAG ALBUM_ID: 24
11-17 00:50:17.427  V/TAG ALBUM_ID: 24
11-17 00:50:17.437  V/TAG ALBUM_ID: 6
11-17 00:50:17.437  V/TAG ALBUM_ID: 22
11-17 00:50:17.447  V/TAG ALBUM_ID: 6
11-17 00:50:17.457  V/TAG ALBUM_ID: 24
11-17 00:50:17.467  V/TAG ALBUM_ID: 10
11-17 00:50:17.467  V/TAG ALBUM_ID: 10
11-17 00:50:17.477  V/TAG ALBUM_ID: 24
11-17 00:50:17.477  V/TAG ALBUM_ID: 19
11-17 00:50:17.477  V/TAG ALBUM_ID: 24
11-17 00:50:17.487  V/TAG ALBUM_ID: 24
11-17 00:50:17.487  V/TAG ALBUM_ID: 24
11-17 00:50:17.497  V/TAG ALBUM_ID: 24
11-17 00:50:17.497  V/TAG ALBUM_ID: 23
11-17 00:50:17.507  V/TAG ALBUM_ID: 24
11-17 00:50:17.527  V/TAG ALBUM_ID: 24
11-17 00:50:17.557  V/TAG ALBUM_ID: 24
11-17 00:50:17.577  V/TAG ALBUM_ID: 24
11-17 00:50:17.587  V/TAG ALBUM_ID: 24
11-17 00:50:17.587  V/TAG ALBUM_ID: 6
11-17 00:50:17.597  V/TAG ALBUM_ID: 24
11-17 00:50:17.597  V/TAG ALBUM_ID: 24
11-17 00:50:17.607  V/TAG ALBUM_ID: 18
11-17 00:50:17.607  V/TAG ALBUM_ID: 24
11-17 00:50:17.607  V/TAG ALBUM_ID: 24
11-17 00:50:17.617  V/TAG ALBUM_ID: 24
11-17 00:50:17.617  V/TAG ALBUM_ID: 17
11-17 00:50:17.627  V/TAG ALBUM_ID: 17
11-17 00:50:17.627  V/TAG ALBUM_ID: 16
11-17 00:50:17.637  V/TAG ALBUM_ID: 24
11-17 00:50:17.647  V/TAG ALBUM_ID: 13
11-17 00:50:17.647  V/TAG ALBUM_ID: 12
11-17 00:50:17.657  V/TAG ALBUM_ID: 12
11-17 00:50:17.657  V/TAG ALBUM_ID: 24
11-17 00:50:17.667  V/TAG ALBUM_ID: 18
11-17 00:50:17.667  V/TAG ALBUM_ID: 9
11-17 00:50:17.677  V/TAG ALBUM_ID: 24
11-17 00:50:17.677  V/TAG ALBUM_ID: 18
11-17 00:50:17.677  V/TAG ALBUM_ID: 24
11-17 00:50:17.687  V/TAG ALBUM_ID: 15
11-17 00:50:17.687  V/TAG ALBUM_ID: 10
11-17 00:50:17.697  V/TAG ALBUM_ID: 26
11-17 00:50:17.697  V/TAG ALBUM_ID: 6
11-17 00:50:17.697  V/TAG ALBUM_ID: 16
11-17 00:50:17.707  V/TAG ALBUM_ID: 16
11-17 00:50:17.707  V/TAG ALBUM_ID: 15
11-17 00:50:17.717  V/TAG ALBUM_ID: 20
11-17 00:50:17.717  V/TAG ALBUM_ID: 17
11-17 00:50:17.717  V/TAG ALBUM_ID: 6
11-17 00:50:17.727  V/TAG ALBUM_ID: 11
11-17 00:50:17.727  V/TAG ALBUM_ID: 11
11-17 00:50:17.737  V/TAG ALBUM_ID: 13
11-17 00:50:17.737  V/TAG ALBUM_ID: 16
11-17 00:50:17.737  V/TAG ALBUM_ID: 18
11-17 00:50:17.747  V/TAG ALBUM_ID: 22
11-17 00:50:17.747  V/TAG ALBUM_ID: 10
11-17 00:50:17.757  V/TAG ALBUM_ID: 21
11-17 00:50:17.757  V/TAG ALBUM_ID: 24
11-17 00:50:17.767  V/TAG ALBUM_ID: 10
11-17 00:50:17.767  V/TAG ALBUM_ID: 8
11-17 00:50:17.767  V/TAG ALBUM_ID: 8
11-17 00:50:17.777  V/TAG ALBUM_ID: 13
11-17 00:50:17.777  V/TAG ALBUM_ID: 15
11-17 00:50:17.787  V/TAG ALBUM_ID: 18
11-17 00:50:17.787  V/TAG ALBUM_ID: 8
11-17 00:50:17.787  V/TAG ALBUM_ID: 15
11-17 00:50:17.797  V/TAG ALBUM_ID: 19
11-17 00:50:17.797  V/TAG ALBUM_ID: 18
11-17 00:50:17.807  V/TAG ALBUM_ID: 10
11-17 00:50:17.807  V/TAG ALBUM_ID: 24
11-17 00:50:17.817  V/TAG ALBUM_ID: 24
11-17 00:50:17.817  V/TAG ALBUM_ID: 15
11-17 00:50:17.827  V/TAG ALBUM_ID: 22
11-17 00:50:17.827  V/TAG ALBUM_ID: 24
11-17 00:50:17.827  V/TAG ALBUM_ID: 15
11-17 00:50:17.837  V/TAG ALBUM_ID: 18
11-17 00:50:17.837  V/TAG ALBUM_ID: 17
11-17 00:50:17.847  V/TAG ALBUM_ID: 10
11-17 00:50:17.847  V/TAG ALBUM_ID: 16
11-17 00:50:17.847  V/TAG ALBUM_ID: 15
11-17 00:50:17.857  V/TAG ALBUM_ID: 15
11-17 00:50:17.857  V/TAG ALBUM_ID: 11
11-17 00:50:17.867  V/TAG ALBUM_ID: 11
11-17 00:50:17.867  V/TAG ALBUM_ID: 19
11-17 00:50:17.877  V/TAG ALBUM_ID: 15
11-17 00:50:17.877  V/TAG ALBUM_ID: 26
11-17 00:50:17.887  V/TAG ALBUM_ID: 24
11-17 00:50:17.887  V/TAG ALBUM_ID: 19
11-17 00:50:17.897  V/TAG ALBUM_ID: 13
11-17 00:50:17.897  V/TAG ALBUM_ID: 9
11-17 00:50:17.907  V/TAG ALBUM_ID: 26
11-17 00:50:17.907  V/TAG ALBUM_ID: 14
11-17 00:50:17.917  V/TAG ALBUM_ID: 15
11-17 00:50:17.917  V/TAG ALBUM_ID: 14
11-17 00:50:17.927  V/TAG ALBUM_ID: 6
11-17 00:50:17.927  V/TAG ALBUM_ID: 24
11-17 00:50:17.937  V/TAG ALBUM_ID: 20
11-17 00:50:17.947  V/TAG ALBUM_ID: 9
11-17 00:50:17.957  V/TAG ALBUM_ID: 19
11-17 00:50:17.957  V/TAG ALBUM_ID: 24
11-17 00:50:17.967  V/TAG ALBUM_ID: 24
11-17 00:50:17.967  V/TAG ALBUM_ID: 24
11-17 00:50:17.967  V/TAG ALBUM_ID: 12
11-17 00:50:17.977  V/TAG ALBUM_ID: 26
11-17 00:50:17.977  V/TAG ALBUM_ID: 24
11-17 00:50:17.987  V/TAG ALBUM_ID: 23
11-17 00:50:17.997  V/TAG ALBUM_ID: 18
11-17 00:50:17.997  V/TAG ALBUM_ID: 12
11-17 00:50:18.007  V/TAG ALBUM_ID: 11
11-17 00:50:18.017  V/TAG ALBUM_ID: 16
11-17 00:50:18.027  V/TAG ALBUM_ID: 12
11-17 00:50:18.027  V/TAG ALBUM_ID: 12
11-17 00:50:18.047  V/TAG ALBUM_ID: 16
11-17 00:50:18.057  V/TAG ALBUM_ID: 10
11-17 00:50:18.057  V/TAG ALBUM_ID: 18
11-17 00:50:18.087  V/TAG ALBUM_ID: 15
11-17 00:50:18.097  V/TAG ALBUM_ID: 24
11-17 00:50:18.097  V/TAG ALBUM_ID: 22
11-17 00:50:18.107  V/TAG ALBUM_ID: 24
11-17 00:50:18.117  V/TAG ALBUM_ID: 24
11-17 00:50:18.117  V/TAG ALBUM_ID: 14
11-17 00:50:18.127  V/TAG ALBUM_ID: 24
11-17 00:50:18.127  V/TAG ALBUM_ID: 2
11-17 00:50:18.127  V/TAG ALBUM_ID: 24
11-17 00:50:18.137  V/TAG ALBUM_ID: 24
11-17 00:50:18.137  V/TAG ALBUM_ID: 24
11-17 00:50:18.147  V/TAG ALBUM_ID: 21
11-17 00:50:18.147  V/TAG ALBUM_ID: 12
11-17 00:50:18.157  V/TAG ALBUM_ID: 18
11-17 00:50:18.157  V/TAG ALBUM_ID: 14
11-17 00:50:18.167  V/TAG ALBUM_ID: 14
11-17 00:50:18.317  V/TAG ALBUM_ID: 24
11-17 00:50:18.317  V/TAG ALBUM_ID: 20
11-17 00:50:18.327  V/TAG ALBUM_ID: 18
11-17 00:50:18.327  V/TAG ALBUM_ID: 20
11-17 00:50:18.337  V/TAG ALBUM_ID: 24
11-17 00:50:18.337  V/TAG ALBUM_ID: 18
11-17 00:50:18.347  V/TAG ALBUM_ID: 11
11-17 00:50:18.347  V/TAG ALBUM_ID: 12
11-17 00:50:18.347  V/TAG ALBUM_ID: 21
11-17 00:50:18.357  V/TAG ALBUM_ID: 8
11-17 00:50:18.357  V/TAG ALBUM_ID: 26
11-17 00:50:18.367  V/TAG ALBUM_ID: 26
11-17 00:50:18.367  V/TAG ALBUM_ID: 8
11-17 00:50:18.367  V/TAG ALBUM_ID: 17
11-17 00:50:18.377  V/TAG ALBUM_ID: 8
11-17 00:50:18.377  V/TAG ALBUM_ID: 23
11-17 00:50:18.377  V/TAG ALBUM_ID: 6
11-17 00:50:18.387  V/TAG ALBUM_ID: 23
11-17 00:50:18.387  V/TAG ALBUM_ID: 24
11-17 00:50:18.397  V/TAG ALBUM_ID: 9
11-17 00:50:18.397  V/TAG ALBUM_ID: 10
11-17 00:50:18.397  V/TAG ALBUM_ID: 20
11-17 00:50:18.407  V/TAG ALBUM_ID: 22
11-17 00:50:18.407  V/TAG ALBUM_ID: 14
11-17 00:50:18.417  V/TAG ALBUM_ID: 8
11-17 00:50:18.417  V/TAG ALBUM_ID: 8
11-17 00:50:18.427  V/TAG ALBUM_ID: 10
11-17 00:50:18.427  V/TAG ALBUM_ID: 9
11-17 00:50:18.437  V/TAG ALBUM_ID: 24
11-17 00:50:18.437  V/TAG ALBUM_ID: 8
11-17 00:50:18.437  V/TAG ALBUM_ID: 8
11-17 00:50:18.447  V/TAG ALBUM_ID: 13
11-17 00:50:18.447  V/TAG ALBUM_ID: 19
11-17 00:50:18.457  V/TAG ALBUM_ID: 24
11-17 00:50:18.477  V/TAG ALBUM_ID: 8
11-17 00:50:18.487  V/TAG ALBUM_ID: 24
11-17 00:50:18.487  V/TAG ALBUM_ID: 6
11-17 00:50:18.497  V/TAG ALBUM_ID: 26
11-17 00:50:18.497  V/TAG ALBUM_ID: 24
11-17 00:50:18.507  V/TAG ALBUM_ID: 26
11-17 00:50:18.517  V/TAG ALBUM_ID: 20
11-17 00:50:18.517  V/TAG ALBUM_ID: 12
11-17 00:50:18.527  V/TAG ALBUM_ID: 12
11-17 00:50:18.527  V/TAG ALBUM_ID: 24
11-17 00:50:18.527  V/TAG ALBUM_ID: 16
11-17 00:50:18.557  V/TAG ALBUM_ID: 15
11-17 00:50:18.567  V/TAG ALBUM_ID: 24
11-17 00:50:18.567  V/TAG ALBUM_ID: 17
11-17 00:50:18.577  V/TAG ALBUM_ID: 8
11-17 00:50:18.577  V/TAG ALBUM_ID: 17
11-17 00:50:18.587  V/TAG ALBUM_ID: 13
11-17 00:50:18.587  V/TAG ALBUM_ID: 20
11-17 00:50:18.597  V/TAG ALBUM_ID: 24
11-17 00:50:18.607  V/TAG ALBUM_ID: 8
11-17 00:50:18.607  V/TAG ALBUM_ID: 23

1 个答案:

答案 0 :(得分:0)

我将在这里做出有根据的猜测。其中一些专辑没有URI。这对我的代码来说不是问题。由其他音乐播放器定位的专辑封面只是从位于专辑目录中的图像文件中提取出来的。

修改

不,这只是部分答案。