2017-04-04 75 views
0

你好Devs我是新來的,我真的想要一個幫助或建議,我創建了音樂應用程序,我有歌曲列表視圖與專輯artWork,但它加載非常慢,我想使用asyncTask加載位圖感謝您的幫助.....在AsyncTask的背景下加載位圖

這裏是我的代碼

public class SongFragment extends Fragment { 


private Context context; 
private SongsAdapter songAdt; 
private ListView songView; 
private ArrayList<Songs> songsList; 
//Binder 
private boolean musicBound = false; 
//Service 
private MusicService musicSrv; 
private Intent playIntent; 
//activity and playback pause flags 
private boolean playbackPaused = false; 
//connect to the service 
private ServiceConnection musicConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     MusicService.MusicBinder binder = (com.example.android.materialmusic.MusicService.MusicBinder) service; 
     //get service 
     musicSrv = binder.getService(); 
     //pass list 
     musicSrv.setList(songsList); 
     musicBound = true; 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     musicBound = false; 
    } 
}; 

public SongFragment() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(final LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_song, container, false); 

    songView = (ListView) rootView.findViewById(R.id.songs); 
    //instantiate list 
    songsList = new ArrayList<>(); 
    //get songs from device 
    getSongList(); 
    //sort alphabetically by title 
    Collections.sort(songsList, new Comparator<Songs>() { 
     public int compare(Songs a, Songs b) { 
      return a.getTitle().compareTo(b.getTitle()); 
     } 
    }); 

    //create and set adapter 
    songAdt = new SongsAdapter(getActivity(), songsList); 
    songView.setAdapter(songAdt); 

    songView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      musicSrv.setSong(Integer.parseInt(view.getTag().toString())); 
      musicSrv.playSong(); 
      if (playbackPaused) { 
       playbackPaused = false; 
      } 
     } 
    }); 
    return rootView; 
} 

//start and bind the service when the activity starts 
@Override 
public void onStart() { 
    super.onStart(); 
    if (playIntent == null) { 
     playIntent = new Intent(getActivity(), MusicService.class); 
     this.getActivity().bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE); 
     this.getActivity().startService(playIntent); 
    } 
} 

//method to retrieve song_item info from device 
public void getSongList() { 
    //query external audio 
    ContentResolver musicResolver = getActivity().getContentResolver(); 
    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    @SuppressLint("Recycle") 
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 

    if (musicCursor != null && musicCursor.moveToFirst()) { 
     //get columns 
     int titleColumn = musicCursor.getColumnIndex 
       (MediaStore.Audio.Media.TITLE); 
     int idColumn = musicCursor.getColumnIndex 
       (MediaStore.Audio.Media._ID); 
     int artistColumn = musicCursor.getColumnIndex 
       (MediaStore.Audio.Media.ARTIST); 
     int albumColumn = musicCursor.getColumnIndexOrThrow 
       (MediaStore.Audio.Media.ALBUM_ID); 
     //add songs to list 
     do { 
      long thisId = musicCursor.getLong(idColumn); 
      String thisTitle = musicCursor.getString(titleColumn); 
      String thisArtist = musicCursor.getString(artistColumn); 

      long thisAlbum = musicCursor.getLong(albumColumn); 
      Uri sArtworkUri = Uri 
        .parse("content://media/external/audio/albumart"); 
      Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, thisAlbum); 

      Bitmap artWork = null; 
      try { 
       artWork = MediaStore.Images.Media.getBitmap(
         musicResolver, albumArtUri); 
       artWork = Bitmap.createScaledBitmap(artWork, 150, 150, true); 

      } catch (FileNotFoundException exception) { 
       exception.printStackTrace(); 
       artWork = BitmapFactory.decodeResource(getResources(), 
         R.drawable.no_cover); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      songsList.add(new Songs(thisId, thisTitle, thisArtist, artWork)); 
     } 
     while (musicCursor.moveToNext()); 
    } 
} 

private class LoadingBitmap extends AsyncTask<Void, Void, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(Void... params) { 
     return bitmap; 
    } 
} 

} 
+3

不要使用的AsyncTask使用[畢加索](http://square.github.io/picasso/)如果你是從一個url加載。 –

回答

0

您可以使用Glide library像這樣在你的代碼:

ImageView imageView = (ImageView) findViewById(R.id.my_image_view); 
Glide.with(this).load("your image url").into(imageView); 

它很容易和方便使用,將有助於執行你的應用程序更好。

+0

如何在我的分段活動中添加滑行庫 – Pranav

+0

使用上面提到的代碼或有一個演示可用的庫。你可以在這裏與我分享你的代碼,我可以幫你。 –

+0

謝謝我會盡快通知您 – Pranav

0

我會建議使用Glide你的目的,它提供了所有當前所需的工具,如獲取圖像並調整它的大小和緩存。

最好是你自己讀,否則我需要實現整個解決方案。

+0

感謝您的回答,我想問,我們不需要在滑行庫中使用AsyncTask – Pranav

0

我發現最好的方法來獲取專輯封面

 Glide.with(mContext) 
      .load(getAlbumArtUri(song.getArtWork())) 
      .placeholder(R.drawable.no_cover) 
      .override(120,120) 
      .into(holder.albumArt); 

private static Uri getAlbumArtUri(long albumId) { 
    return ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumId); 
}