java - 如何在 Android 应用程序中离线使用图像?

标签 java android mysql android-volley picasso

我有一个应用程序,即使没有可用的互联网,我也需要用户查看图像。最初,我向 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);
}

最佳答案

使用了 Android 通用图像加载器,这是缓存中存储的最佳图像

Declare

    private ImageLoader imageLoader1;

On Create

 imageLoader1 = ImageLoader.getInstance();

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

no_image here a drawable image without any image load in Cache

   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 进行编译 ....

关于java - 如何在 Android 应用程序中离线使用图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38671038/

相关文章:

java - 将 .equals() 替换为 Objects.equals()

java - 是否有 mybatis 'pre-interceptor' 在sql执行之前以编程方式更改sql?

android - PubNub 映射错误

MySQL 通配符匹配任意字符或不匹配

mysql - group_concat 值大于特定值

JavaFX 多次运行线程

java - JTable - 如何为特定列中的每一行插入新的 JLabel?

android - 错误 : Program "/NDK-build" not found in PATH

android - 膨胀自定义按钮类时出错

mysql - 找到正确的主键