如何使用Asynctask进行位图?

时间:2018-01-07 20:14:13

标签: java android bitmap android-asynctask

我正在将我的mp3文件中的图像转换为 Bitmap [] ,但它会导致应用程序停止一点,我想使用Asynctask使其更顺畅。

这就是我得到位图的方式:

  Bitmap[] bm = new Bitmap[mySongs.size()];       
  for (int i = 0; i < mySongs.size(); i++) {
        if (mySongs.size() > 0) {
        u = Uri.parse(mySongs.get(i).toString());
        mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(getApplicationContext(), u);      
        byte[] artBytes = mediaMetadataRetriever.getEmbeddedPicture();  
        if(artBytes != null) {
            InputStream is = new ByteArrayInputStream(mediaMetadataRetriever.getEmbeddedPicture());
            bm[i] = BitmapFactory.decodeStream(is);         
        }  
   }

如何将它放入Asynctask?

1 个答案:

答案 0 :(得分:0)

这就是您的实现方式:

bio#about

我建议创建一个扩展 AsyncTask myAsyncTask = new AsyncTask() { @Override protected Object doInBackground(Object[] objects) { //ToDo: Do all the work in here and return result as object return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); //ToDo: Do whatever you want to do with the result in here } } 的静态类,或者不传递任何上下文或者可能发生内存泄漏

相关问题