Best way to download image from url and save it in internal memory

I am developing an application in which I want to download an image from a URL. I need to immediately download these images and save them in the internal storage. To download more than 200 images. Please tell me the best way to download these images in minimum time. If any library of the third part is available, please inform.

+5
source share
2 answers

Consider using Picasso for your purpose. I use it in one of my projects. To save the image to an external drive, you can use the 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) { } } ); 

Here you can download this library.

+19
source

You can download the th image from the url, for example:

 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 then you can save the image like this:

 FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg"); fos.write(response); fos.close(); 
+5
source

All Articles