【问题标题】:How to make images available offline in an android app?如何在 android 应用程序中使图像离线可用?
【发布时间】:2016-07-30 06:28:57
【问题描述】:

我有一个应用程序,即使没有可用的互联网,我也需要用户查看图像。最初,我向 mysql 数据库发出 volley 请求,并使用解析后的数据通过 Picasso 显示图像。

Picasso.with(context).load(magazineVersions.get(position).getMagazine_image_url()).resize(400, 500).into(holder.iv_magazine);  

现在我还响应我的 volley 请求下载图像并将它们存储在名为 .images 的文件夹中,如下所示:-

public ArrayList showJSON(String json) {
    ParseJSON pj = new ParseJSON(json);
    pj.parseJSON();

    image_url = ParseJSON.img_url;
    mag_version = ParseJSON.magversion;
    download_path = ParseJSON.download_url;
    sample_url = ParseJSON.sample_url;

    for (String img : image_url) {
        downloadImages(img);
    }

    ArrayList android_version = new ArrayList<>();

    for (int i = 0; i < image_url.length; i++) {
        MagazineVersion magazineVersion = new MagazineVersion();
        magazineVersion.setMagazine_version_name(mag_version[i]);
        magazineVersion.setMagazine_image_url(image_url[i]);
        magazineVersion.setDownload_url(download_path[i]);
        magazineVersion.setSample_url(sample_url[i]);
        android_version.add(magazineVersion);
    }
    return android_version;
}

downloadImages 的代码如下:-

public void downloadImages(String img_url) {
    String fileName = img_url.substring(img_url.lastIndexOf('/') + 1, img_url.length());
    File file = new File("/storage/emulated/0/.shatayushi/.images" + fileName);

    if (file.exists() && !file.isDirectory()) {
        Log.d("ImageExists", fileName);

    }else
        new DownloadImagesAsync().execute(img_url);
}

我想知道如何使用下载的图像而不是存储在服务器上的图像。简单如下:-

if(Images exist in .images folder)
{
    //use picasso to display the images stored in the device 
}else
{
    //use picasso to display the images stored on the server
Picasso.with(context).load(magazineVersions.get(position).getMagazine_image_url()).resize(400, 500).into(holder.iv_magazine);
}

【问题讨论】:

标签: java android mysql android-volley picasso


【解决方案1】:

使用 Android Universal Image-Loader 这是最好的图像存储在缓存中

声明

    private ImageLoader imageLoader1;

创建时

 imageLoader1 = ImageLoader.getInstance();

 imageLoader1.init(ImageLoaderConfiguration.createDefault(getActivity()));

no_image 这里是一个可绘制的图像,缓存中没有任何图像加载

   DisplayImageOptions
            options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .showImageOnLoading(R.drawable.no_image) // resource or drawable
            .showImageForEmptyUri(R.drawable.no_image) // resource or drawable
            .showImageOnFail(R.drawable.no_image)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    imageLoader1.displayImage(yourpath.replace(" ", "%20"), ivprofile, options);

您也可以使用 Piccaso 来将此存储图像存储在缓存中用于此也可以使用 okhttp... 使用此...

 if (imageURL != null) {
Picasso.with(this).load(imageURL).error(R.drawable.no_Image)
.into(ivImage);
}

也可以用 Okhttp 编译 ....

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    相关资源
    最近更新 更多