Android firebase resize image and upload to the firebase storage

Viewed 2365

I want to resize the image before upload it to the firebase storage.

My problem is: FireBase uploads the images base on its Uri. I got the image from the gallery intent and place it on the ImageView and resize it like this :

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

    switch(requestCode) {
        case RC_SELECT_PHOTO:
            if (resultCode == RESULT_OK) {

                mImageUri = returnIntentData.getData();
                mSelectedImage.setImageURI(mImageUri);

                //get a Image obj from ImageView

                Bitmap bit = ((BitmapDrawable) mSelectedImage.getDrawable()).getBitmap();

                int width = bit.getWidth();
                int height = bit.getHeight();
                float rate = 0.0f;

                int maxResolution = 1920;

                int newWidth = width;
                int newHeight = height;

                if(width > height) {
                    if(maxResolution < width) {
                        rate = maxResolution/ (float) width;
                        newHeight = (int) (height * rate);
                        newWidth = maxResolution;
                    }

                } else {
                    if(maxResolution < height) {
                        rate = maxResolution/(float)height;
                        newWidth = (int) (width * rate);
                        newHeight = maxResolution;
                    }
                }

                Bitmap resizedImage = Bitmap.createScaledBitmap(bit, newWidth, newHeight, true);

                mSelectedImage.setImageBitmap(resizedImage);

            }

and firebase upload it base on the Uri like this (if there are other ways to upload file, it will be so helpful!):

public void uploadPhotoToStorage() {
    //present image's Ref
    StorageReference presentGymPhotoRef =
            mGymStorageReferences.child(mImageUri.getLastPathSegment());
    //Upload file to Firebase Storage
    presentGymPhotoRef.putFile(mImageUri)
            .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri downloadUri = taskSnapshot.getDownloadUrl();
                    //save gymPhoto Uri to instance

                    gymPhotoUri = downloadUri.toString();
                    gymName = mEditGymName.getText().toString();

.......

Is there any way to get the Bitmap's Uri or the ImageView's Uri? Or just resize and upload this successfully?

1 Answers
Related