0

I am using a Glide library for loading remote URLs into ImageView's. I want to save the image from this ImageView to gallery. (I don't want to make another network call again to download the same image).

How we can achieve this?

mikeswright49
  • 3,381
  • 19
  • 22
C.P.
  • 1,182
  • 3
  • 13
  • 32
  • Possible duplicate of [How does one use glide to download an image into a bitmap?](http://stackoverflow.com/questions/27394016/how-does-one-use-glide-to-download-an-image-into-a-bitmap) – Nongthonbam Tonthoi Jun 08 '16 at 11:28

4 Answers4

0
Glide.with(yourApplicationContext))
    .load(youUrl)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
            //set bitmap to imageview and save 
        }
    };
Ihor Bykov
  • 1,843
  • 3
  • 15
  • 22
  • I dont want to set image to imageview & save to gallery at a time. Actually I have a recylcerview for all images. Each item of recyclerview has option to download that image. On selection download option, I want to retrieve image from ImageView (as Bitmap maybe) so that I can save it to gallery. – C.P. Jun 08 '16 at 11:55
0
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
   Bitmap bitmap = drawable.getBitmap();
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);

    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
    String fileName = "image.jpeg";
    File file = new File("your_directory_path/"
            + fileName);
    try {
        file.createNewFile();
        // write the bytes in file
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(byteArrayOutputStream.toByteArray());
        // remember close the FileOutput stream
        fileOutputStream.close();
        ToastHelper.show(getString(R.string.qr_code_save));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        ToastHelper.show("Error");
    }

Note : If your drawble is not always an instanceof BitmapDrawable

            Bitmap bitmap;
        if (mImageView.getDrawable() instanceof BitmapDrawable) {
            bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
        } else {
            Drawable d = mImageView.getDrawable();
            bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

Try this

darwin
  • 1,524
  • 1
  • 22
  • 32
0

I haven't try this way. But i think this match your problem. Put this code on onBindViewHolder of your RecyclerView adapter.

Glide.with(yourApplicationContext))
    .load(youUrl)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
            //Set bitmap to your ImageView 
            imageView.setImageBitmap(bitmap);

            viewHolder.saveButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                           //Save bitmap to gallery
                           saveToGallery(bitmap);
                        }
                    });
        }
    };
ikhsanudinhakim
  • 1,554
  • 16
  • 23
0

This might help you

public void saveBitmap(ImageView imageView) {

    Bitmap bitmap = ((GlideBitmapDrawable) imageView.getDrawable()).getBitmap();
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/My Images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception ex) {
        //ignore
    }
}
AabidMulani
  • 2,325
  • 1
  • 28
  • 47