[英]How to make images available offline in an android app?
我有一個應用程序,即使沒有互聯網,我也需要用戶查看圖像。 最初,我向mysql數據庫發出凌空請求,然后使用解析的數據通過Picasso顯示圖像。
Picasso.with(context).load(magazineVersions.get(position).getMagazine_image_url()).resize(400, 500).into(holder.iv_magazine);
現在,我還根據我的截擊請求下載圖像,並將它們存儲在名為.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 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編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.