Android Picasso - 在 ImageView 中查看之前将图像保存在本地 [英] Android Picasso - Save image locally before viewing it in ImageView

查看:62
本文介绍了Android Picasso - 在 ImageView 中查看之前将图像保存在本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个显示图像列表的应用程序.但在此之前,我将所有图像下载到 SD 卡,并显示一个包含下载文件数量的进度对话框.由于要下载许多图像,我正在考虑使用 Picasso 优化该过程.所以没有进度对话框.使用毕加索,我想在图像显示在 ImageView 之前将图像保存在我的应用程序目录中(作为普通下载的文件).我想使用此代码:

I have made an app which shows a list of image. But before, I downloaded all the images to the sdcard, and I show a progress dialog with the number of downloaded files. I'm thinking to optimise the process using Picasso because of many images to download. So no progress dialog. With Picasso, I would like to save the image in my app directory (as a normal downloaded file) before it is shown in the ImageView. I thought to use this code :

Picasso picassoInstance = new Picasso.Builder(context)
 .downloader(new CustomDownloader(context, destination))
 .build();

自定义下载器:

public class CustomDownloader extends OkHttpDownloader {

    private File destination;

    public CustomDownloader(Context context, File destination){
        super(context);
        this.destination = destination;
    }

    @Override
    public Response load(Uri uri, boolean localCacheOnly) throws IOException {
        Response response = super.load(uri, networkPolicy);

        FileOutputStream fileOutput = new FileOutputStream(destination);
        InputStream inputStream = response.getInputStream();

        byte[] buffer = new byte[1024];
        int bufferLength;
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();

        return response;
    }
}

最后,我使用这个语句在 ImageView 中加载图像:

And finally, I use this statement to load the image in the ImageView :

picassoInstance.load(item.getItem().getPath()).resize(width,0).into(imageView);

我使用这种实现是否正确?或者你能提供另一种方式吗?

Am I correct to use this kind of implementation ? Or do you can provide another way ?

编辑 1:我使用上面的代码,但图像没有显示在 ImageView 中,即使我看到在 SDCARD 中下载的图像.

EDIT 1 : I use the code above but the image is not shown in the ImageView, even if I see the images downloaded in the SDCARD.

编辑 2:使用此代码,使用新线程:

EDIT 2 : With this code, using new Thread :

@Override
public Response load(Uri uri, boolean localCacheOnly) throws IOException {
    Response response = super.load(uri, networkPolicy);
    new Thread(new Runnable() {
        try {
            FileOutputStream fileOutput = new FileOutputStream(destination);
            InputStream inputStream = response.getInputStream();

            byte[] buffer = new byte[1024];
            int bufferLength;
            while ((bufferLength = inputStream.read(buffer)) > 0) {
                fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.close();
        } catch(IOException e){
            e.printStackTrace();
        }
    }).start();
    return response;
}

我得到了一个 java.lang.ArrayIndexOutOfBoundsException: src.length=2048 srcPos=2048 dst.length=1024 dstPos=0 length=1024 在这一行 while ((bufferLength = inputStream.read(buffer)) > 0)

I got an java.lang.ArrayIndexOutOfBoundsException: src.length=2048 srcPos=2048 dst.length=1024 dstPos=0 length=1024 in this line while ((bufferLength = inputStream.read(buffer)) > 0)

推荐答案

使用 picasso 加载图片是不错的,有如下几个优点:

using picasso to load image is good, there are a serveral advantage like:

  1. 解压缩图像的内存和磁盘缓存,后处理
  2. 自动创建的单例图像下载器
  3. 一次下载多个

但是你可以尝试使用 Volley,对我来说这是最好的!关于你的问题,尝试在logcat中显示item.getItem().getPath(),看看路径是否正常,如果你想使用sdcard中的图像,路径应该是sdcard中的路径

But you can try to use Volley, for me this is the best! About your problem, try to show item.getItem().getPath() in logcat and look if the path is ok or not, the path should be the path in sdcard if you want to use the image in sdcard

这篇关于Android Picasso - 在 ImageView 中查看之前将图像保存在本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆