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"));
        }
    }
}

これがlogcatです-

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 に答える