RealBadCoder
RealBadCoder

Reputation: 351

Android Studio: How to resize an image before uploading to server?

I have an app that takes a picture, assigns it to an imageview and then uploads it to a parse server. The problem is that I need the images to be 200 x 200 px before upload. Is there a way to do this programatically? this is my function so far:

 public void Myfunction (){


    //image
    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
    byte[] imageInByte = baos.toByteArray();
    ParseFile file = new ParseFile("myimage.jpg",imageInByte);

Could anyone help me please?

Upvotes: 2

Views: 4587

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57203

To scale bitmap down before compress, add

bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false));

If your bitmap is not square, you probably want to crop it before scaling

Upvotes: 4

Related Questions