如何在后台进程中获取所有音频文件(异步)?

时间:2016-07-26 17:36:51

标签: android android-asynctask android-viewpager cursor

我有以下代码但是加载需要太多时间,因为有时应用程序崩溃。我如何在后台处理。我尝试使用AsyncTask,但我无法在其中放入以下代码。请记住,必须在代码的这一部分返回膨胀的View'view'。

View view = inflater.inflate(R.layout.activity_second, container, false);
            ArrayList<Song> songList = new ArrayList<Song>();
            final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            final String[] cursor_cols = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DURATION };
            final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
            final Cursor cursor = view.getContext().getContentResolver().query(uri, cursor_cols, where, null, null);
            while (cursor.moveToNext()) {
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                String track = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                String data = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
                try {
                    MediaStore.Images.Media.getBitmap(view.getContext().getContentResolver(), albumArtUri);
                } catch (FileNotFoundException exception) {
                    exception.printStackTrace();
                    albumArtUri = Uri.parse("Unknown");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Collections.sort(songList, new Comparator<Song>() {
                    public int compare(Song a, Song b) {
                        return a.getTitle().compareTo(b.getTitle());
                    }
                });
                songList.add(new Song(albumId, track, artist, album, albumArtUri.toString()));
                RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new GridLayoutManager(view.getContext(),2));
                RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(view.getContext(), songList);
                recyclerView.setAdapter(recyclerViewAdapter);
                registerForContextMenu(recyclerView);

            }
            return view;

1 个答案:

答案 0 :(得分:1)

您正在重复设置} 循环中的RecyclerView

试试这个:

        View view = inflater.inflate(R.layout.activity_second, container, false);
        ArrayList<Song> songList = new ArrayList<Song>();
        final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        final String[] cursor_cols = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DURATION };
        final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
        final Cursor cursor = view.getContext().getContentResolver().query(uri, cursor_cols, where, null, null);
        while (cursor.moveToNext()) {
            String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
            String track = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            String data = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
            int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
            Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
            try {
                MediaStore.Images.Media.getBitmap(view.getContext().getContentResolver(), albumArtUri);
            } catch (FileNotFoundException exception) {
                exception.printStackTrace();
                albumArtUri = Uri.parse("Unknown");
            } catch (IOException e) {
                e.printStackTrace();
            }
            Collections.sort(songList, new Comparator<Song>() {
                public int compare(Song a, Song b) {
                    return a.getTitle().compareTo(b.getTitle());
                }
            });
            songList.add(new Song(albumId, track, artist, album, albumArtUri.toString()));
        }

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(view.getContext(),2));
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(view.getContext(), songList);
        recyclerView.setAdapter(recyclerViewAdapter);
        registerForContextMenu(recyclerView);

        return view;

如果这仍然太慢,那么位图检索很可能是罪魁祸首。我可以向您展示如何在该部分上放置AsyncTask。