1- Add compile 'com.squareup.picasso:picasso:2.5.2'
inside build.gradle or download jar file from Picasso
2- Picasso.with(this)
.load("Your image url here")
.into(target);
If you list then you can also use above during traversing your list(inside any loop).
3-As target in above class displaying compile time error. Put following code as your inner class.
private Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/image1.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 80, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}}
}).start(); }
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {} }};
4-If you are still facing problem in understanding above code please refer following example. Picasso - Image downloading and caching library for Android
5- ` for(int i = 0; i < cons.length(); i++)
{
JSONObject c = cons.getJSONObject(i);
imgURL = c.getString(image);
Picasso.with(this)
.load(imgURL)
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/image1.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}`