如何在Android中处理加载图片

6

可能是重复问题:
Android - 如何在ListView中进行图像的懒加载

我已经尝试了几天,但似乎无法理解这个过程。我的应用程序从服务器访问多个图像。目前,它设置为一次加载并显示1张图片。当用户点击下一个按钮时,将加载并显示下一张图片。但加载时间有点太长了。有什么方法可以改善下一张图片的加载时间?

我一直在使用线程和AsyncTask。我的想法是也将上一个和下一个图像保存在内存中。当用户点击下一个时,我执行以下操作:

prevImage = currentImage;
currentImage = nextImage;
nextImage = getBitmapfromURL(urlPath);

而 nextImage 实际上是在 AsyncTask 或 Thread 中执行的。我的问题是,如果用户在该线程完成之前点击了下一个按钮,则会显示空白图像。所以我不确定这是否是正确的方法。是否有其他方法可以提高这些图像的加载时间?


请参考这个问题:https://dev59.com/Y3RB5IYBdhLWcg3wtZMV,在这里你可以找到很多想法,人们提出了不同的方法来实现这个目标。 - Sankar Ganesh PMP
谢谢回复。那段代码看起来和Rohit发布的一模一样。但我不知道如何将那个为List视图设计的示例转换成我的设计,只显示一个图像。我添加了ImageLoader和Utils类。然后我在我的类的onCreate方法中添加了imageLoader = new ImageLoader(getApplicationContext()); imageLoader.DisplayImage(firstImages[0], this, imgView); 来加载第一张图片,但是我的图片没有显示出来。有什么想法吗? - Brian
找到问题了。你必须使用imgView.setTag(url);没有设置标签,它不会在下载后更新imageView。感谢指引我到这个线程。还有感谢Rohit。我只需要看一下他如何使用ImageLoader类就好了。 - Brian
我很高兴看到你解决了问题,好朋友。 - Sankar Ganesh PMP
好吧,我猜我说得太早了。我确实让它工作了一会儿。但是在浏览几张图片后,我遇到了内存不足错误。在Fedor的代码顶部,他评论说你应该改用SoftReferences。我尝试过并编译成功了,但现在无法加载任何图像。我以前从未使用过Soft References,所以我肯定做错了。您如何在他的代码中实现Soft References?那应该消除OOM错误,对吗?谢谢。 - Brian
1个回答

1
这是我从服务器加载图像的类。
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.HashMap;
import java.util.Stack;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;

import com.com.app.R;

public class ImageLoader {

    // the simplest in-memory cache implementation. This should be replaced with
    // something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
    private HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();

    private File cacheDir;

    public ImageLoader(Context context) {
        // Make the background thead low priority. This way it will not affect
        // the UI performance
        photoLoaderThread.setPriority(Thread.NORM_PRIORITY - 1);

        // Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED))
            cacheDir = new File(
                    android.os.Environment.getExternalStorageDirectory(),
                    "LazyList");
        else
            cacheDir = context.getCacheDir();
        if (!cacheDir.exists())
            cacheDir.mkdirs();
    }

    final int stub_id = R.drawable.no_image;

    public void DisplayImage(String url, Activity activity, ImageView imageView) {
        if (cache.containsKey(url))
            imageView.setImageBitmap(cache.get(url));
        else {
            queuePhoto(url, activity, imageView);
            imageView.setImageResource(stub_id);
        }
    }

    private void queuePhoto(String url, Activity activity, ImageView imageView) {
        // This ImageView may be used for other images before. So there may be
        // some old tasks in the queue. We need to discard them.
        photosQueue.Clean(imageView);
        PhotoToLoad p = new PhotoToLoad(url, imageView);
        synchronized (photosQueue.photosToLoad) {
            photosQueue.photosToLoad.push(p);
            photosQueue.photosToLoad.notifyAll();
        }

        // start thread if it's not started yet
        if (photoLoaderThread.getState() == Thread.State.NEW)
            photoLoaderThread.start();
    }

    private Bitmap getBitmap(String url) {
        // // I identify images by hashcode. Not a perfect solution, good for
        // the
        // // demo.
        // String filename = String.valueOf(url.hashCode());
        // File f = new File(cacheDir, filename);
        //
        // // from SD cache
        // Bitmap b = decodeFile(f);
        // if (b != null)
        // return b;

        // from web
        try {
            return new BitmapDrawable(new URL(url).openStream()).getBitmap();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {

        }

        return null;
    }

    // Task for the queue
    private class PhotoToLoad {
        public String url;
        public ImageView imageView;

        public PhotoToLoad(String u, ImageView i) {
            url = u;
            imageView = i;
        }
    }

    PhotosQueue photosQueue = new PhotosQueue();

    public void stopThread() {
        photoLoaderThread.interrupt();
    }

    // stores list of photos to download
    class PhotosQueue {
        private Stack<PhotoToLoad> photosToLoad = new Stack<PhotoToLoad>();

        // removes all instances of this ImageView
        public void Clean(ImageView image) {
            for (int j = 0; j < photosToLoad.size();) {
                if (photosToLoad.get(j).imageView == image)
                    photosToLoad.remove(j);
                else
                    ++j;
            }
        }
    }

    class PhotosLoader extends Thread {
        public void run() {
            try {
                while (true) {
                    // thread waits until there are any images to load in the
                    // queue
                    if (photosQueue.photosToLoad.size() == 0)
                        synchronized (photosQueue.photosToLoad) {
                            photosQueue.photosToLoad.wait();
                        }
                    if (photosQueue.photosToLoad.size() != 0) {
                        PhotoToLoad photoToLoad;
                        synchronized (photosQueue.photosToLoad) {
                            photoToLoad = photosQueue.photosToLoad.pop();
                        }
                        Bitmap bmp = getBitmap(photoToLoad.url);
                        cache.put(photoToLoad.url, bmp);

                        if (photoToLoad.url == null) {
                            photoToLoad.url = "http://192.168.0.2/phpdemoprojects/cardsonmobile/uploads/cards/0009.png";
                        } else {

                            if (((String) photoToLoad.imageView.getTag())
                                    .equals(photoToLoad.url)) {
                                BitmapDisplayer bd = new BitmapDisplayer(bmp,
                                        photoToLoad.imageView);
                                Activity a = (Activity) photoToLoad.imageView
                                        .getContext();
                                a.runOnUiThread(bd);
                            }
                        }
                    }
                    if (Thread.interrupted())
                        break;
                }
            } catch (InterruptedException e) {
                // allow thread to exit
            }
        }
    }

    PhotosLoader photoLoaderThread = new PhotosLoader();

    // Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        ImageView imageView;

        public BitmapDisplayer(Bitmap b, ImageView i) {
            bitmap = b;
            imageView = i;
        }

        public void run() {
            if (bitmap != null)
                imageView.setImageBitmap(bitmap);
            else
                imageView.setImageResource(stub_id);
        }
    }

    public void clearCache() {
        // clear memory cache
        cache.clear();

        // clear SD cache
        File[] files = cacheDir.listFiles();
        for (File f : files)
            f.delete();
    }

}

所以基本上,您将所有图像存储到SD卡中,然后在需要时访问它们,对吗?那么,为了按照这种方式进行操作,我必须让用户等待图像下载完成,然后才能让他们点击下一个/上一个按钮。我认为我不想这样做,因为这可能需要几分钟的时间来加载。感谢您的回复。 - Brian
不,用户不必等待下载所有图片。将图像URL提供给它后,它将在后台继续下载并在需要时立即显示它们。我将其与自定义列表适配器一起使用。 - Rohit Mandiwal
我理解你的用法。但是,根据我想要做的事情,我不希望在点击按钮查看图片之前就加载图片。例如,我有一个屏幕,其中包含Spinner项目和不同的类别。然后,当用户单击按钮时,会出现一个新屏幕,显示基于Spinner中选择的类别的若干张图片。因此,在查看图片之前,我不知道要加载哪些图片,这意味着我必须等待它们加载完才能查看下一组图片。如果我还没有正确理解您的意思,请让我知道。再次感谢。 - Brian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接