0

I had a problem where some of my images are loaded inside imageview rotated. I solved the problem by loading an image using Glide.

The image is loaded inside 100 x 100 imageview. Can I retrieve the image which is stored inside that imageview as 100 x 100 and upload it to the server instead of sending the original image?

AlwaysConfused
  • 729
  • 2
  • 12
  • 37
  • Possible duplicate of [Getting Image from ImageView](http://stackoverflow.com/questions/9042932/getting-image-from-imageview) – Niki van Stein May 17 '17 at 14:49

1 Answers1

1

Yes you can

public byte[] getImageFromImageView(ImageView imageView,String filePath) {
    Drawable imageDrawable = imageView.getDrawable();
    Bitmap imageBitmap = ((BitmapDrawable)imageDrawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

Then upload file to server or you can create RequestBodyand send it to server if you using Retrofit.

armanso
  • 144
  • 3
  • 9
  • Yes @AlwaysConfused, if you want to upload to server you should mention which Library are you using so I can help you about that too – armanso May 17 '17 at 15:00
  • Well I am using firebase storage to upload, so I just need to send bytes or an InputStream – AlwaysConfused May 17 '17 at 15:08
  • update the codes but I rather to save it file and read it again from file with OutputStream with this kinda thing you have it in you memory and if it's big image you may see out of memory error you can use `getCacheDir()` and set delete on exist flag to true – armanso May 17 '17 at 15:17
  • Well probably is a good idea to save and instantly delete the file as well after its been uploaded or check if the image is not too large and just crop it. Thanks anyway for the code, I will try it now – AlwaysConfused May 17 '17 at 17:02
  • the problem I am getting is that the bitmap saves black image now. Thats because I load the image inside the imageview using Glide – AlwaysConfused May 17 '17 at 18:17
  • are sure that you run code after image loaded to imageview? because it doesn't matter which library you're using, at end it will call one of imageview's `setImageBitmap` || `setImageDrawable` || `setImageIcon` function – armanso May 17 '17 at 19:33