如何从内部存储器查询uri的歌曲

时间:2013-12-11 15:44:50

标签: android android-mediaplayer android-file

我想访问歌曲详细信息,包括artistalbumdurationtitledisplay name等。

可以通过查询EXTERNAL_CONTENT_URI外部存储中的文件来完成此操作,但我无法使用INTERNAL_CONTENT_URI从内部存储中查询文件。

过去两天我一直坚持这一点。这是我的代码:

void loadPlayList(){
    ArrayList<Song> list = null;
    try{
        list = new ArrayList<Song>();
        Uri uri;
       //if(SplashScreen.getDirPrefs())
            uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
       //else
       //Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;

        String[] m_data = {MediaStore.Audio.Media.ALBUM_ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DURATION,MediaStore.Audio.Media.TRACK,MediaStore.Audio.Media.DISPLAY_NAME};
        Cursor c = getApplicationContext().getContentResolver().query(uri, m_data, MediaStore.Audio.Media.IS_MUSIC+"=1", null, null);

        PlayerManager pm = new PlayerManager();

        //Log.e("vmplayer loadPlayList ", "cursor : "+c.getCount());
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext())//while(c.moveToNext())
        {
            String str = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            str = str.substring(0, (str.length() - 4));

            for(int i = 0; i < GlobalClass.songList.size(); i++)
            {
                Song sn = GlobalClass.songList.get(i);
                //Log.e("vmplayer loadPlayList ", "str : "+str);
                if(sn.name.equalsIgnoreCase(str))
                {
                    sn.title = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                    sn.album = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                    sn.albumId = c.getLong(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                    sn.artist = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                    sn.duration = pm.milliSecondsToTimer((long)(c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION))));
                    //Log.e("vmplayer loadPlayList ", "dur : "+sn.duration);
                    Bitmap art = null;
                    try{
                        Uri contentUri = Uri.parse("content://media/external/audio/albumart");
                        Uri albumUri = ContentUris.withAppendedId(contentUri,c.getLong(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID)));

                        ContentResolver res = getApplicationContext().getContentResolver();
                        InputStream in = res.openInputStream(albumUri);
                        art = BitmapFactory.decodeStream(in);
                        //art = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(),albumUri);
                        //art = Bitmap.createScaledBitmap(art, 100, 100, true);
                    }
                    catch(FileNotFoundException e){
                        Log.e("vmplayer loadPlayList ", "err1 :"+e);
                        art = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.artwork);
                    }
                    catch(Exception e){
                        Log.e("vmplayer loadPlayList err2", ""+e);
                        art = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.artwork);
                    }

                    //Log.e("vmplayer loadPlayList art", "bit map : "+art);
                    sn.coverImg = art;
                    list.add(sn);
                }
            }
        }

        GlobalClass.songList.clear();
        GlobalClass.songList = list;
        Log.e("loadPlayList", "cont : "+GlobalClass.songList.size());
        c.close();
    }
    catch(Exception e){
        Log.e("vmplayer loadPlayList err", ""+e);
    }

}

提前致谢..

1 个答案:

答案 0 :(得分:0)

我认为这是由您的Android版本造成的。

我写了与你相同的代码。它适用于Android 5.0.1,但在Android 2.3.6上崩溃。我认为如果您的Android版本太旧,您应该尝试使用其他方法在内部存储中进行查询。