1

I use the following code to obtain a bitmap from an ImageView. This image is not saved anywhere else on my device. I want to upload this image into an online mysqli database. However, to do so I need to decrease the size of the file first. I found a lot of links about this, however they all require the file to be saved on the device and then use FileOutputStream. I am looking for a way to reduce the file size so that it can be comfortably transferred using the Volley API ( i am currently receiving either run out of memory exceptions or broken pipe errors). Hence I am looking for a way to modify this code to be able to significantly decrease the file size, whilst still maintaining a quality which can be comfortably shown on a mobile device. The original image is taken straight from the camera hence the size is quite large. Here is my code:

ImageView pic_holder = (ImageView) findViewById(R.id.picturedisplay);
            Bitmap bitmap = ((BitmapDrawable)pic_holder.getDrawable()).getBitmap();
            ByteArrayOutputStream stream=new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
            byte[] image=stream.toByteArray();
            String img_str = Base64.encodeToString(image, 0);

I would like to decrease the size of the img_str which is passed to my Volley method.

Alk
  • 5,215
  • 8
  • 47
  • 116
  • `Bitmap.CompressFormat.PNG` is lossless, it will ignore the quality setting. – Rafael Feb 23 '16 at 02:55
  • If you are OK with lossy compression, you could consider using JPEG compression instead. You can play with the quality parameter and see if there is a quality degradation/size trade off. – greenrobo Feb 23 '16 at 02:58
  • I was looking more for something like this however without the FileStream component http://stackoverflow.com/questions/16954109/reduce-the-size-of-a-bitmap-to-a-specified-size-in-android – Alk Feb 23 '16 at 03:08
  • Why are you getting the Bitmap from ImageView? – Tin Megali Feb 23 '16 at 04:24
  • Does the bitmap need to be transferred in the same width/height dimensions as you got it from the ImageView? – Doug Stevenson Feb 23 '16 at 04:32
  • `need to decrease the size of the file first`. ? Where is the file? You have a Bitmap.you said. – greenapps Feb 23 '16 at 07:06
  • `This image is not saved anywhere`. Then how did it display in the ImageView? Where did it come from? – greenapps Feb 23 '16 at 07:07
  • Why would you use base64 encoding if you want to reduce the amount of bytes? It increases them with 30 %. – greenapps Feb 23 '16 at 07:09
  • @DougStevenson it doesn't, this is an image from the phone's camera. I just need to display it on an android phone in the future in a decent quality, but I'm sure some phones take pictures which are much higher quality then required for that purpose. Greenaps, it comes from the camera, right after it is taken I load it into the imageView. – Alk Feb 23 '16 at 07:31
  • Well, you'll need to define what "decent quality" actually means, then figure out how far you can downsize your image in order to maintain this standard of quality when displayed again on another screen. Compression isn't really the main point here, it's how far your can scale down the image dimensions. – Doug Stevenson Feb 23 '16 at 07:55

0 Answers0