2016-07-26 4 views
0

私は次のコードを持っていますが、アプリケーションがクラッシュすることがありますがロードに時間がかかりすぎます。私はバックグラウンドプロセスでやります。私はそれをAsyncTaskを使ってやろうとしましたが、その中に次のコードを置くことができませんでした。膨らんだビュー 'ビュー'はコードのその部分に戻さなければならないことに注意してください。バックグラウンド処理(非同期)ですべてのオーディオファイルを取得するには?

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

あなたは繰り返しループ内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をその部分だけに置く方法を教えてください。

+0

それは助けにはなりませんでしたが、間違いなくその改善を置いていました。私はそれがクラッシュする場所を見つけた特定のポイントがあります。実際には、このコードをフラグメントのonCreateViewに入れました。私はタブを使用しているので、タブを変更して戻ると、アプリケーションがハングしてクラッシュします。 –

関連する問題