0

I have an app where user takes a picture and the picture is loaded into the ImageView and then saved in FirebaseStorage. I have problems with fetching images from Firebase (using Picasso) when the internet is slow. How to reduce size of the Bitmap before converting to byte array and saving to Firebase?

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity().RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageView.setImageDrawable(makeImageRounded(imageBitmap));
        detailViewModel.uploadPhotoToFirebase(imageBitmap); 
    }
}
Angelina
  • 1,473
  • 2
  • 16
  • 34
  • Use `Bitmap.compress()`. Cf an [older question](https://stackoverflow.com/questions/4579647/how-to-save-a-jpeg-image-on-android-with-a-custom-quality-level). – Pontus Gagge Feb 06 '18 at 11:54
  • Thanks, I will try. I am new to this topic, that's why I have missed this older question – Angelina Feb 06 '18 at 11:58
  • the `"data"` extra is supposed to be a thumbnail, see [Taking Photos Simply](https://developer.android.com/training/camera/photobasics.html) for a complete explanation, including how to scale down the image. – bwt Feb 06 '18 at 12:02
  • I am using Bitmap.compress() function with different formats (JPEG, PNG) and different quality (50-30) and then checking the size of the resulting image in the backend Firebase. These parameters seem to have no effect on the file size. It is somewhere between 80-100KB. Can I compress further? – Angelina Feb 06 '18 at 13:50

1 Answers1

0

Set Permission in the App

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Declare imageView and uri

private Uri filePath;
private final int PICK_IMAGE_REQUEST = 71;

Firebase storage declare

FirebaseStorage storage;
StorageReference storageReference;

Initilize in onCreate

storage = FirebaseStorage.getInstance(); storageReference = storage.getReference();

OnClick of button

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

onActivityResult you will get the file path

@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 )
  {
    filePath = data.getData();
    try {
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
      imageView.setImageBitmap(bitmap);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

Upload image to firebase

private void uploadImage() {

        if(filePath != null)
        {
            StorageReference ref = storageReference.child("images/"+ UUID.randomUUID().toString());
            ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            Toast.makeText(this, "Uploaded", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {


                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        }
                    });
        }
    }

Reduce size of specific bitmap to save on firebase

 public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float)width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

call methods - Bitmap converetdImage = getResizedBitmap(bitmap, 500);

Adil
  • 812
  • 1
  • 9
  • 29
  • Thank you very much. I know how to upload image to firebase. The problem is: the images get downloaded slowly when I am not on good internet. My main idea is to make them smaller BEFORE saving to Firebase. I am new to image processing, so I was wondering how to do it – Angelina Feb 06 '18 at 12:58