anup anup - 4 months ago 20
Java Question

How to use Picasso to open a image stored locally in android?

I want my app to be able to retrieve and display images from the device storage when there is no internet available.So when the app loads for the 1st time with internet I download the images into a folder and later when there is no internet I ask it to open from the local storage.
My images are stored at this location


/storage/emulated/0/shatayushi/shatayushi_cover3.jpg


On volley response I call the following code which is used to set the adapter for the recyclerview.

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);
}

File sh_images = new File(Environment.getExternalStorageDirectory()+"/shatayushi");
String[] img_files = sh_images.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename)
{
if (filename.toLowerCase().endsWith(".jpg"))
{
return true;
}else {
return false;
}
}
});

String stored_imgurl[]= new String[img_files.length];

for (int i=0; i<img_files.length;i++)
{
stored_imgurl[i]="/storage/emulated/0/shatayushi/"+img_files[i];
}

Log.d("StoredImages",stored_imgurl[0]);


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]);
magazineVersion.setStored_imgurl(stored_imgurl[i]);
android_version.add(magazineVersion);
}
return android_version;

}


And on response of volley I call the above method as follows:-

ArrayList androidVersions = showJSON(response);

DataAdapter adapter = new DataAdapter(DisplayMagazine.this, androidVersions);
recyclerView.setAdapter(adapter);


And inside my DataAdapter I use Picasso to display the images as follows:-

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


This is a part of my log cat:-


07-30 13:44:12.633 16766-16766/com.inevitablesol.shatayushiapp
I/dalvikvm: DexOpt: illegal method access (call
Landroid/support/v7/widget/RecyclerView$ViewHolder;.isUpdated ()Z from
Lcom/inevitablesol/shatayushiapp/adapter/DataAdapter$ViewHolder;)
07-30 13:44:12.633 16766-16766/com.inevitablesol.shatayushiapp
I/dalvikvm: Could not find method
android.support.v7.widget.RecyclerView$ViewHolder.isUpdated,
referenced from method
com.inevitablesol.shatayushiapp.adapter.DataAdapter$ViewHolder.access$super
07-30 13:44:12.633 16766-16766/com.inevitablesol.shatayushiapp
W/dalvikvm: VFY: unable to resolve virtual method 17450:
Landroid/support/v7/widget/RecyclerView$ViewHolder;.isUpdated ()Z


Which says illegal method access and Could not find method
android.support.v7.widget.RecyclerView$ViewHolder.isUpdated
, referenced from method
com.inevitablesol.shatayushiapp.adapter.DataAdapter$ViewHolder.access$super
.

Can anyone please help me with a solution I am new to programming.Thank you.

Answer

you can use like below way to LOAD FILE FROM SDCARD/Device storage: in your case you have to identify if file is available in storage or not. then decide to load from web or storage

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);

make sure you are passing fullpath of image

read from documentaion