Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取所有音乐文件_Java_Android - Fatal编程技术网

Java 获取所有音乐文件

Java 获取所有音乐文件,java,android,Java,Android,我正在尝试获取手机上所有歌曲的所有音乐文件。我正在使用mediastore获取所有歌曲标题、艺术家等。如何获取所有音乐文件? 活动类别: public void getSongList() { //query external audio ContentResolver musicResolver = getContentResolver(); Uri musicUri = android.provider.MediaStore.Audio.Med

我正在尝试获取手机上所有歌曲的所有音乐文件。我正在使用mediastore获取所有歌曲标题、艺术家等。如何获取所有音乐文件? 活动类别:

public void getSongList() {
        //query external audio
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
        //iterate over results if valid
        if (musicCursor != null && musicCursor.moveToFirst()) {
            //get columns
            int titleColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.ARTIST);
            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                songList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (musicCursor.moveToNext());
        }
    }

我也有和你一样的问题。现在我用下面的代码来解决这个问题

   /**
 * It Not used AnyWhere in Project but it's Helpful while you need to get all mp3 file in sdcard.
 * it's nothing but just for future used
 *
 */
public void getAllSongs() {

    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    String[] STAR=null;
    Cursor cursor = getActivity().managedQuery(allsongsuri, STAR, selection, null, null);
    //or
    //Cursor cursor = getActivity().getContentResolver().query(allsongsuri, null, null, null, selection);




    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                String song_name = 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));
                String Duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

                Log.e(TAG, "Song Name ::"+song_name+" Song Id :"+song_id+" fullpath ::"+fullpath+" Duration ::"+Duration);

            } while (cursor.moveToNext());

        }
        cursor.close();

    }
}
这段代码在我的应用程序中运行良好。在这段代码中,我们刚刚在日志中打印了整首歌曲的路径和名称。但是您太聪明了,所以您可以根据需要更改这段代码

我希望你明白我的想法。
祝你好运现在怎么了?您在该光标中没有看到任何内容或异常吗?@mbelsky请检查我的编辑。非常感谢您,我遇到了问题,当我将音乐添加到dowload时,我的应用程序中没有更新音乐。我使用ContentResolver,它在我重新启动genimation时更新。当我向dowload添加音乐时,它将在我的应用程序中更新,你能让我吗?