为什么用Android的MediaStore查不到SD卡的音乐,权限里已经设置了
 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
private ListView list;
private List<Map<String, String>> musicList = 
new ArrayList<Map<String, String>>();
private SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        //musicList=loadSongs();

        list=(ListView) super.findViewById(R.id.listView1);
        adapter = new SimpleAdapter(getApplicationContext(), musicList,
                R.layout.item, new String[] { "name", "artist",
                        "time", "data", "id" }, new int[] {
                        R.id.name, R.id.songer, R.id.time,
                        R.id.path, R.id.musicRating });
        list.setAdapter(adapter);

    }

    public List<Map<String, String>> getmusic(){
    ContentResolver musicResolver = this.getContentResolver();
    Cursor cursor = /*getContentResolver()*/  musicResolver
            .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  
                    new String[] { MediaStore.Audio.Media.TITLE,  
                            MediaStore.Audio.Media.DURATION,  
                            MediaStore.Audio.Media.ALBUM,  
                            MediaStore.Audio.Media.ARTIST,  
                            MediaStore.Audio.Media._ID,  
                            MediaStore.Audio.Media.DATA,  
                            MediaStore.Audio.Media.DISPLAY_NAME }, null,  
                    null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);  

    if (cursor != null && cursor.moveToFirst()) {  
        do {  

            /*String mName = cursor.getString(0);  
            long mTime = cursor.getLong(1);  
            String mAlbum = cursor.getString(2);  
            String mArtist = cursor.getString(3);  
            long mId = cursor.getLong(4);  
            String mData = cursor.getString(5);  */
            String mName =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            int mTime=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
            String mAlbum=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
            String mArtist=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            int mId=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
            String mData=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            String mDisplay = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));

            HashMap a = new HashMap();  
            a.put("name", mName);  
            a.put("time", mTime);  
            //a.put("time1", mTime);  
            a.put("album", mAlbum);  
            a.put("artist", mArtist);  
            a.put("id", mId);  
            a.put("data", mData);  
            a.put("display", mDisplay);

            musicList.add(a);//ArrayList  
            System.out.println(mName + "-->" + mTime + "---->>--" + mData);  
        } while (cursor.moveToNext());  
    }  
    if (!cursor.isClosed()) {  
        cursor.close();  
    }
    return musicList;  
    }   
}

1个回答

内容提供者只能获取到手机SD卡内的音乐,外置SD卡获取不到的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问