简体   繁体   English

Android如何使用AsyncTask将图像下载到sdcard并在完成后显示在右侧项目的listview上?

[英]Android How can i download images with AsyncTask to sdcard and on completion show on listview on the right item?

I want to download some images through AsyncTask to SDcard and on completion to show them on an listview. 我想通过AsyncTask将一些图像下载到SDcard,并在完成后将它们显示在列表视图中。 When the download starts an progress bar is shown, when it stops will show the image downloaded on sdcard. 当下载开始时,将显示一个进度条,当下载停止时,将显示在sdcard上下载的图像。

I saw that are a lot of posts with lazy-load, but what i want is to show a progress bar before showing the image, and i want to be stored on sdcard. 我看到很多帖子都带有延迟加载,但是我想要的是在显示图像之前显示进度栏,并且我想存储在sdcard上。

The bellow code works almost ok, but the problem is that it doesnt show the right picture on the right item all the time when it is downloading the image. 波纹管代码几乎可以正常工作,但是问题在于,下载图像时,它始终不会在正确的项目上显示正确的图片。 I am using the bellow in the adapter: 我在适配器中使用波纹管:

  public View getView(final int position, View convertView, ViewGroup parent) {
        final PooHolder holder; 
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_poo, null);
            holder = new PooHolder();
            holder.tvTime = (TextView)convertView.findViewById(R.id.tvTime);
            holder.tvMessage = (TextView)convertView.findViewById(R.id.tvMessage);
            holder.btnDislike = (ImageButton)convertView.findViewById(R.id.btnDislike);
            holder.btnLike = (ImageButton)convertView.findViewById(R.id.btnLike);
            holder.btnReport = (ImageButton)convertView.findViewById(R.id.btnReport);
            holder.bar = (ProgressBar)convertView.findViewById(R.id.progressBar1);
            holder.bar1 = (ProgressBar)convertView.findViewById(R.id.progressBar2);
            holder.bar2 = (ProgressBar)convertView.findViewById(R.id.progressBar3);
            holder.tvLikes = (TextView)convertView.findViewById(R.id.tvLikes);
            holder.tvComments = (TextView)convertView.findViewById(R.id.tvComments);
            holder.tvDislikes = (TextView)convertView.findViewById(R.id.tvDislike);
            holder.imgPoo = (ImageView)convertView.findViewById(R.id.imgPoo);
            convertView.setTag(holder);
        }else{
            holder = (PooHolder)convertView.getTag();
        }
        final Poo poo = list.get(position);
        String vote = poo.getVote();

        if(poo.getDrawablePath()!=null){
            if(!poos.get(position).isDownloadComplete()){
                Log.i(DEBUG, poo.getMessage());
                holder.bar2.setVisibility(View.VISIBLE);
                holder.imgPoo.setImageBitmap(BitmapFactory.decodeFile(poo.getDrawablePath()));
                holder.imgPoo.setVisibility(View.INVISIBLE);
                if(!poos.get(position).getDownloadState(Poo.DOWNLOAD_START)){
                    DownloadImageTask task = new DownloadImageTask(poo, holder.bar2, holder.imgPoo);
                    task.setOnDownloadListener(new OnDownloadListener(){
                        public void onDownloadStarted() {
                            poos.get(position).startDownload();
                        }
                        public void onDownloadFinished(final Bitmap bmp) {
                            poos.get(position).stopDownload();
                        }
                    });
                    task.execute(poo.getImagePath());
                }
            }else{
                holder.bar2.setVisibility(View.INVISIBLE);
                holder.imgPoo.setVisibility(View.VISIBLE);
                holder.imgPoo.setImageBitmap(BitmapFactory.decodeFile(poo.getDrawablePath()));
                }
            }else{
                holder.bar2.setVisibility(View.INVISIBLE);
                holder.imgPoo.setVisibility(View.VISIBLE);
                holder.imgPoo.setImageResource(R.drawable.icon);
            }
....

The DownloadImageTask.java: DownloadImageTask.java:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    private final String DEBUG = "DownloadImageTask";
    private String url;
    private String imagePath;
    private OnDownloadListener listener;
    private Bitmap bmp;
    private ProgressBar bar;
    private ImageView img;
    private Poo poo;

    public DownloadImageTask(Poo poo, ProgressBar bar, ImageView img) {
        this.img = img;
        this.bar = bar;
        this.poo = poo;
    }

    public void onPreExecute(){
        super.onPreExecute();
        listener.onDownloadStarted();
        bar.setVisibility(View.VISIBLE);
        img.setImageBitmap(BitmapFactory.decodeFile(poo.getDrawablePath()));
        img.setVisibility(View.INVISIBLE);
    }

    public void setOnDownloadListener(OnDownloadListener listener){
        this.listener = listener;
    }

    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(String... params) {
        this.imagePath = poo.getImagePath();
         // params comes from the execute() call: params[0] is the url.
        if(imagePath != null && !imagePath.isEmpty()){
            String file = imagePath.substring(imagePath.lastIndexOf("/") + 1, imagePath.length());
            return BoopoohooUtils.downloadImage(params[0], file);
        }else{
            return null;
        }
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap) {
        Log.i(DEBUG, "bitmap " + bitmap);
        bar.setVisibility(View.INVISIBLE);
        img.startAnimation(BoopoohooUtils.fadeIn());
        img.setVisibility(View.VISIBLE);
        img.setImageBitmap(bitmap);
        listener.onDownloadFinished(bitmap);
    }
}

So to start you will need to use the progress method on the async task class. 因此,开始时,您将需要在异步任务类上使用progress方法。 Link . 链接 Then for your method on the top create a variable for passing through different sequence let's say 5,4,3,2,1. 然后在顶部为您的方法创建一个变量,以通过不同的序列传递,比如说5,4,3,2,1。 When it starts it goes by 5 and decrements to 1 then application will go to post and do whatever gui interactions you are doing. 当它开始时,它递减5并递减到1,然后应用程序将发布并执行您正在执行的gui交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM