【问题标题】:Using AsyncTask to load images into a custom adapter使用 AsyncTask 将图像加载到自定义适配器中
【发布时间】:2012-08-02 21:38:58
【问题描述】:

虽然那里有很多教程,但我发现很难实现 AsyncTask 以将图像从 URI(从内容提供程序获得)加载到自定义适配器中。

我得到了基本的要点,即拥有一个包含 AsyncTask 的类,在 'doInBackground' 中创建位图,然后在 onPostExecute 中设置 ImageView。

对我来说,作为 android 和编程的新手,我的问题是我不知道如何将我的 Uri 传递给每个项目的 AsyncTask、如何创建位图以及如何将其返回给适配器。

我只使用了实际的 AsyncTask 类 (ImageLoader):

package another.music.player;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;

public class ImageLoader extends AsyncTask<String, String, Bitmap> {

    private ImageView imageView;
    private Bitmap bitmap = null;

    @Override
    protected Bitmap doInBackground(String... uri) {

        // Create bitmap from passed in Uri here

        return bitmap;

    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null && imageView != null) {
            imageView.setImageBitmap(bitmap);

        }
    }
}

我的自定义适配器如下所示:

package another.music.player;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore.Audio.AlbumColumns;
import android.provider.MediaStore.Audio.AudioColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

class AlbumAdapter extends CursorAdapter {

    public AlbumAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    private static Uri currentSongUri;

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView albumArt = (ImageView) view.getTag(R.id.albumArt);
        TextView text1 = (TextView) view.getTag(R.id.artistTitle);
        TextView text2 = (TextView) view.getTag(R.id.albumTitle);
        TextView text3 = (TextView) view.getTag(R.id.totalSongs);

        albumArt.setImageBitmap(null);

        text1.setText(cursor.getString(cursor
                .getColumnIndex(AudioColumns.ARTIST)));
        text2.setText(cursor.getString(cursor
                .getColumnIndex(AudioColumns.ALBUM)));
        text3.setText(cursor.getString(cursor
                .getColumnIndex(AlbumColumns.NUMBER_OF_SONGS)));

        String currentAlbumId = cursor.getString(cursor
                .getColumnIndex(BaseColumns._ID));

        Integer currentAlbumIdLong = Integer.parseInt(currentAlbumId);
        Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
        currentSongUri = ContentUris.withAppendedId(artworkUri,
                currentAlbumIdLong);

        //Run ImageLoader AsyncTask here, and somehow retrieve the ImageView & set it.

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.albumitem, null);
        view.setTag(R.id.albumArt, view.findViewById(R.id.albumArt));
        view.setTag(R.id.artistTitle, view.findViewById(R.id.artistTitle));
        view.setTag(R.id.albumTitle, view.findViewById(R.id.albumTitle));
        view.setTag(R.id.totalSongs, view.findViewById(R.id.totalSongs));

        return view;
    }

}

如果有人能告诉我如何进行此操作,我将不胜感激。

谢谢。

【问题讨论】:

  • 您是否开发了这款名为 Shuttle Music Player 的应用程序?
  • 嗨,是的,我做到了。我现在对 AsyncTasks 再熟悉不过了!
  • 您应该在适配器之外使用 AsyncTask,然后在完成后重新填充结果。

标签: android android-asynctask lazy-loading adapter


【解决方案1】:

您需要将您的AsyncTask 传递给视图,以便它可以在完成时对其进行更新:

//Run ImageLoader AsyncTask here, and let it set the ImageView when it is done.
new ImageLoader().execute(view, uri);

并修改AsyncTask,使其可以处理混合参数类型:

public class ImageLoader extends AsyncTask<Object, String, Bitmap> {

    private View view;
    private Bitmap bitmap = null;

    @Override
    protected Bitmap doInBackground(Object... parameters) {

        // Get the passed arguments here
        view = (View) parameters[0];
        String uri = (String)parameters[1];

        // Create bitmap from passed in Uri here
        // ...
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null && view != null) {
            ImageView albumArt = (ImageView) view.getTag(R.id.albumArt);
            albumArt.setImageBitmap(bitmap);
        }
    }
}

我没有测试过这段代码,但它应该能给你一个想法。

【讨论】:

  • 谢谢。我会试一试。唯一的另一件事是我在创建位图时也遇到了麻烦。我不确定如何从 uri 创建位图,因为我似乎无法在没有上下文的情况下使用 decodeInputStream ..
  • 那么您也可以将上下文作为参数传递:new ImageLoader().execute(view, uri, context);Context context = (Context)parameters[2];
  • 所以您是在建议每次 CursorAdapter 执行 bindView() 时都应该假脱机一个新线程?这将为游标中的每一行运行一个线程,如果达到最大池大小,这将耗尽设备电池并可能破坏系统服务
【解决方案2】:

为什么要在 AsyncTask 中执行 setImage?您可以在 线程 中执行此操作。我不认为在这种情况下 AsyncTask 会很好。最好在不同的线程中进行。

【讨论】:

  • AsyncTask 在不同的线程上运行:developer.android.com/reference/android/os/AsyncTask.html
  • 我不一定需要按照我展示的方式进行操作。位图可以只返回到适配器,然后设置图像视图。我愿意接受建议。不过,从我读过的所有内容来看,我确实相信我应该以某种方式使用 AsyncTask。
  • 我知道 AsyncTask 在不同的线程中运行。我不希望在 AsyncTask 中执行此任务。无论如何,如果它满足要求,您可以这样做。
  • 我不确定为什么这个答案被否决,@Debarati 指出在这种情况下使用 AsyncTask 是一种过度杀戮。您不需要线程池或取消,那么为什么不直接使用 Thread 呢?
  • 根据我的阅读,AsyncTask 允许您轻松地与 UI 进行通信,Thread 不能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2016-01-26
  • 2011-07-08
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多