4

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?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Jarvis
  • 41
  • 3

1 Answers1

1

If you just need to upload the file in a certain size so you can retrieve it in the same size you can use Picasso http://square.github.io/picasso/. It allows you to provide a url and resize the image with the height and width dimensions that you may want without having to necessarily resize the image before upload (you may want to for other reasons). Below is an example of something I implemented to get an image from a URL and resize the image to fit inside my ImageView.

/**
 * Binds the data from the json file to the ImageView and the Textviews
 * are part of the movie_layout resource file.
 * @param recyclerViewHolder
 * @param position
 */
@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {
    MovieData movieData = list.get(position);
    Picasso.with(context).load(movieData.getMovie_img_url()).resize(75,100).into(recyclerViewHolder.imageView);
    recyclerViewHolder.textViewTitle.setText(movieData.getMovie_title());
    recyclerViewHolder.textViewAuthor.setText(movieData.getMovie_author());

}

Its simple to implement please view the following code below for an example on how to add it to your project:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.squareup.picasso:picasso:2.5.2' <!--Added Here-->
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

}

Anthony
  • 31
  • 5
  • I'm new at Picasso, so i'd like know how do i get the image's Uri from ViewHolder's ImageView. I understand that Picasso will resize the image but i need the specific Uri that is resized Imageview. and I know how to access the origin image file on sd card or loads image from uri But I don't know how to make the Uri which is resized Image. – Jarvis Aug 11 '17 at 03:20
  • Sorry, i just got the message for this. Why do you need to image to be resized? Picasso allows you to display the image resized, but do you need to store the image with that particular size? – Anthony Aug 12 '17 at 03:42
  • because i need to upload the image to the server(firebase storage) i believe Picasso only display the image isn't it? if Picasso can resize(or compress) the source itself it will be helpful. – Jarvis Aug 12 '17 at 04:22
  • Yes, it will fetch the image and load the image in the ImageView for you. You can resize it it you choose. – Anthony Aug 12 '17 at 04:52
  • The reason I ask is that you have the image stored already, and this process will allow you to show the image resized then there wouldn't really be a need to resize the image to store it. I guess you can do it from memory and there are some answers on stackoverflow for how to accomplish this, but if you can give me more background it would be helpful as I plan to use firebase storage also to store images and display them on the app. – Anthony Aug 12 '17 at 04:59
  • My App is kind of social app. So user can upload image but because of this function, the size of Image is not fixed. It can waste of resource especially network data in storage and device. So i want to resize the image before upload it to the firebase. – Jarvis Aug 13 '17 at 03:15
  • I wonder if this would be helpful for you. I have not set up the Firebase storage just yet, but when I do I can test it. https://stackoverflow.com/questions/4715044/android-how-to-convert-whole-imageview-to-bitmap – Anthony Aug 16 '17 at 16:55
  • This is the link to the android developer notes: https://developer.android.com/reference/android/view/View.html#buildDrawingCache%28boolean%29 – Anthony Aug 16 '17 at 16:59
  • Your solution doesn't address the issue. – MobDev May 10 '20 at 18:26