0

I am getting image from gallery and trying to reduce its quality before uploading it on a server but I am not getting it how to do it correctly.

Below is my code:

 openGallery.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent i = new Intent();
            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(i,"Select picture"),GALLERY_IMAGE);
        }
    });

      @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == GALLERY_IMAGE && resultCode == RESULT_OK && data != null){

        selectedImage = data.getData();

        Bitmap bitmap = null;

        try {

             bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),selectedImage);


        } catch (IOException e) {
            e.printStackTrace();

        }

          if(bitmap !=  null){

              bookImage.setImageBitmap(bitmap);
          }

      }

    else{

        TastyToast.makeText(getApplicationContext(),"No image selected",TastyToast.LENGTH_SHORT,TastyToast.INFO).show();

    }
}

Someone please let me know what should I add in above code to get result any help would be appreciated.

THANKS

Digvijay
  • 2,887
  • 3
  • 36
  • 86

1 Answers1

0

I use code like below to both shrink and set JPG quality. The code below saves the JPG stream to a file, but you should be able to send to a memory stream and to byte array instead.

Having said that, you should also consider having ANDROID return a reduced sized picture to you. With older devices especially, your app may not have enough memory to store a full sized picture. I don't have any code for you to do this though, sorry.

            // Shrink to fit picture in a max size
        Point newSize = util.fitSizeInBounds(new Point(rawBitmap.getWidth(), rawBitmap.getHeight()), new Point(_maxPicSize, _maxPicSize));
        if (rawBitmap.getWidth() > newSize.x || rawBitmap.getHeight() > newSize.y) {
            scaledBitmap = Bitmap.createScaledBitmap(rawBitmap, newSize.x, newSize.y, false);
            // Because I was concerned about memory leaks, I call recycle, but maybe this is not nessessary
            rawBitmap.recycle(); rawBitmap = null;
        }
        else {
            scaledBitmap = rawBitmap; rawBitmap = null;
        }

        // Save file
        try {
            FileOutputStream outStream = new FileOutputStream(_tempDir + fileName);
            if (ext.equals("png")) scaledBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            else scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 70, outStream);  // Here you can compress JPG (70% quality here)
            outStream.close();
        }
        catch (Exception e) {}



public Point fitSizeInBounds(Point size, Point bounds) {

    // return size that can fit in bounds.  Result will keep same proportion as orig size
    int newWidth = size.x;
    int newHeight = size.y;

    if (newWidth > bounds.x) {
        newHeight = (newHeight * bounds.x) / newWidth;
        newWidth = bounds.x;
    }
    if (newHeight > bounds.y) {
        newWidth = (newWidth * bounds.y) / newHeight;
        newHeight = bounds.y;
    }
    return new Point(newWidth, newHeight);
}
Ernie Thomason
  • 1,579
  • 17
  • 20