0

I've already succeeded in fetching an image from the galary but I'm not able to reduce the size of the image and save it in separate directory. so how can i reduce its size in kbs and save it?

please help.

My code is:

public class PickImageActivity extends AppCompatActivity {

private int PICK_IMAGE_REQUEST = 1;

ImageView iv;
Button imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pick_image);


    imageButton = (Button) this.findViewById(R.id.btnPicker);

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pickImage();
        }
    });
}

private void pickImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            ImageView iv = (ImageView) findViewById(R.id.iv1);
            iv.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

1 Answers1

0

There are a lot of solutions out there, you can use Glide to resize image:

Bitmap bm=Glide  
    .with(context)
    .load(imageUrl)
    .override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
    . into(-1,-1).get();

or try this solutions to write the resized bitmap

  1. How to compress images
  2. How to compress image size

And after that save the bitmap where you need it

Community
  • 1
  • 1
KlajdPaja
  • 959
  • 1
  • 8
  • 17