0

我正在尝试使用此代码获取播放列表。

final String MEDIA_PATH=new String(MediaStore.Audio.Media.IS_MUSIC+"!=0");  
    //final String MEDIA_PATH=new String(MediaStore.Video.Media.IS_PRIVATE);
            private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


    // Constructor
    public SongsManager(){

    }

    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }

    /**
     * Class to filter files which are having .mp3 & .mp4 extension
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP4"));
        }
    }
}

这是日志猫——

02-09 10:09:34.749: E/AndroidRuntime(389): Caused by: java.lang.NullPointerException 
02-09 10:09:34.749: E/AndroidRuntime(389): at com.example.androidtablayout.SongsManager.getPlayList(SongsManager.java:29) 
02-09 10:09:34.749: E/AndroidRuntime(389): at com.example.androidtablayout.AudioActivity.onCreate(AudioActivity.java:29) 
02-09 10:09:34.749: E/AndroidRuntime(389): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-09 10:09:34.749: E/AndroidRuntime(389): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我正在按照你的建议使用你的代码 Grishu,但是在这个错误中,我使用它的方式如下所示:

public class SongsManager {
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private Cursor musiccursor, videocursor;
// projection for audio media files
    String[] music = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE };
    // projection for video media files
    String[] video = { MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE };

    int totalSongs,totalSongs1;
    public ArrayList<HashMap<String, String>>getPlayList(){
        // set cursor for audio media
        musiccursor= managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                music, null, null, null);
        // set cursor for video media
        videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                video, null, null, null);

        totalSongs=musiccursor.getCount();
        totalSongs1=videocursor.getColumnCount();

         if (musiccursor!= null) {
                if (musiccursor.moveToFirst()) {
                    do {
                        String songname = musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                        int song_id = musiccursor.getInt(musiccursor.getColumnIndex(MediaStore.Audio.Media._ID));
                        String fullpath = musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                        songsList.addAll(songsList);
                        String albumname =musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                        int album_id = musiccursor.getInt(musiccursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                        String artistname = musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                        int artist_id = musiccursor.getInt(musiccursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));

                     } while (musiccursor.moveToNext());
                }
                musiccursor.close();
            }
         else if (videocursor!=null) {
            if(videocursor.moveToFirst()){
                do{
                    String songname= videocursor.getString(videocursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
                    int song_id= videocursor.getInt(videocursor.getColumnIndex(MediaStore.Video.Media._ID));
                    String fullpath=videocursor.getString(videocursor.getColumnIndex(MediaStore.Video.Media.DATA));

                    songsList.addAll(songsList);
                    String albumname =videocursor.getString(videocursor.getColumnIndex(MediaStore.Video.Media.ALBUM));
                   // int album_id = videocursor.getInt(videocursor.getColumnIndex(MediaStore.Video.Media.ALBUM_ID));
                    String artistname = videocursor.getString(videocursor.getColumnIndex(MediaStore.Video.Media.ARTIST));
                   // int artist_id = videocursor.getInt(videocursor.getColumnIndex(MediaStore.Video.Media.ARTIST_ID));

                }while (videocursor.moveToNext());
            }
            videocursor.close();
        }
         return songsList;
}


    private Cursor managedQuery(Uri externalContentUri, String[] audio2,
            Object object, Object object2, Object object3) {
        // TODO Auto-generated method stub
        return null;
    }

      }
4

1 回答 1

0

尝试这个 :

 String[] STAR = { "*" };
   int totalSongs;
 public void ListAllSongs() 
  {
    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    if (MusicUtils.isSdPresent()) {
        cursor = managedQuery(allsongsuri, STAR, selection, null, null);
        totalSongs = cursor.getCount();
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    String songname = cursor
                            .getString(cursor                                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    int song_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media._ID));
                    String fullpath = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DATA));
                    fullsongpath.add(fullpath);
                    String albumname = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                    int album_id = cursor
                            .getInt(cursor
                                    .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                    String artistname = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    int artist_id = cursor
                            .getInt(cursor
                                    .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
                                            } while (cursor.moveToNext());
            }
            cursor.close();
        }
    }
 }

编辑:

您查看以下链接以在列表视图中显示存储在您的 SD 卡中的所有视频文件。

1)链接 1
2)链接 2
3)链接 3

我希望它会帮助你。

谢谢。

于 2013-02-09T05:08:46.430 回答