Umesh Kumar Saraswat
Umesh Kumar Saraswat

Reputation: 768

Best way to download image from URL and save it into internal storage memory

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.

Upvotes: 13

Views: 41812

Answers (4)

louisHoang
louisHoang

Reputation: 9

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

Upvotes: 0

Ahmet Faruk Çuha
Ahmet Faruk Çuha

Reputation: 43

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...

Upvotes: 0

Uniruddh
Uniruddh

Reputation: 4436

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.

Upvotes: 35

João Marcos
João Marcos

Reputation: 3972

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

Upvotes: 9

Related Questions