I am developing an app in which I want to download image from URL. I need to download these images at once and stored it into internal storage. There are more than 200 images for downloading. Please tell me the best way to download these images at minimum time possible. If any third part library is available the please tell.
Asked
Active
Viewed 4.1k times
13
-
You may use Picasso or Volley – Ankit Bansal Apr 14 '15 at 11:03
-
picasso does not save these images into internal storage. it directly shows on ui – Umesh Kumar Saraswat Apr 14 '15 at 11:05
-
1You can use Volley lib and create a custom Request that write your image directly in the device memory on success response. – Bubu Apr 14 '15 at 11:07
-
you might want to take a look at this post of mine http://stackoverflow.com/questions/15549421/how-to-download-and-save-an-image-in-android – Droidman Apr 14 '15 at 12:33
-
Use AQuery for this. It store images in both memory cache and local storage. check: https://code.google.com/p/android-query/wiki/ImageLoading – asad.qazi Apr 14 '15 at 12:49
-
You want to download 200 at a single shot or you want to implement lazy loading of images in listview? – Paresh Mayani Apr 14 '15 at 12:52
4 Answers
35
Consider using Picasso for your purpose. I'm using it in one of my project. To save image on external disk you can use following:
Picasso.with(mContext)
.load(ImageUrl)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/yourDirectory");
if (!myDir.exists()) {
myDir.mkdirs();
}
String name = new Date().toString() + ".jpg";
myDir = new File(myDir, name);
FileOutputStream out = new FileOutputStream(myDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch(Exception e){
// some action
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
);
From here you can download this library.

Airon Tark
- 8,900
- 4
- 23
- 19

Uniruddh
- 4,427
- 3
- 52
- 86
-
1i want to download 200 images. for that i need to loop to download these images – Umesh Kumar Saraswat Apr 14 '15 at 11:45
-
-
1@Signo: Check the answer of Dhawal Shoda parmar, in this link: http://stackoverflow.com/a/15918369/2571277 – Uniruddh Dec 04 '15 at 16:03
-
1@astuter thank you very much for the quick response! :) I tried but with no luck, it says there's nothing if the ExternalStorage, now I'll do a little bit of debug to find out what's the issue, thanks again :) – LS_ Dec 04 '15 at 16:23
-
1
-
@UmeshKumarSaraswat you can retrieve the image from local file system as `Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView)`; – Harika Choudary Kanikanti Aug 06 '16 at 17:55
-
Better to create name as String name = new Date().getTime() + ".jpg"; if you have morethan 1 image to download at a time. – Sachin Varma Feb 14 '17 at 07:47
9
You can download th image from an url like this:
URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
And you may then want to save the image so do:
FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg");
fos.write(response);
fos.close();

João Marcos
- 3,872
- 1
- 19
- 14
0
BitmapDrawable bitmapDrawable = (BitmapDrawable) holder.post_image.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bitmap, "IMG_" + Calendar.getInstance().getTime(), null);
It is quite a simple job...

Ahmet Faruk Çuha
- 43
- 4
0
const actionDownloadImage = (urls) => {
urls.map((url) => {
const splitUrl = url.split("/");
const filename = splitUrl[splitUrl.length - 1];
fetch(url)
.then((response) => {
response.arrayBuffer().then(function (buffer) {
const url = window.URL.createObjectURL(new Blob([buffer]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", filename); //or any other extension
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
})
.catch((err) => {
console.log(err);
});
});
}

louisHoang
- 9
- 2