0

How to downsize images before uploading to Firebase? or after

This is my first app https://play.google.com/store/apps/details?id=com.devkang.mystep

I think it takes too long to upload and download.

private void sendPost() {
    mProgressDialog.setMessage("Sending post...");
    mProgressDialog.setCancelable(false);
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.show();

    FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".", ","))
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    final String postId = FirebaseUtils.getUid();
                    TextView postDialogTextView = (TextView) mRootView.findViewById(R.id.post_dialog_edittext);
                    String text = postDialogTextView.getText().toString();

                  //add hashTag
                    EditText tag = (EditText) mRootView.findViewById(R.id.post_dialog_hash_tag);
                    String hashTag = tag.getText().toString();

                    mPost.setUser(user);
                    mPost.setNumComments(0);
                    mPost.setNumLikes(0);
                    mPost.setTimeCreated(System.currentTimeMillis());
                    mPost.setPostId(postId);
                    mPost.setPostText(text);
                    mPost.setPostHashTag(hashTag);

                    if (mSelectedUri != null) {
                        FirebaseUtils.getImageSRef()
                                .child(mSelectedUri.getLastPathSegment())
                                .putFile(mSelectedUri)
                                .addOnSuccessListener(getActivity(),
                                        new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                            @Override
                                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                                String url = Constants.POST_IMAGES + "/" + mSelectedUri.getLastPathSegment();
                                                mPost.setPostImageUrl(url);
                                                addToMyPostList(postId);
                                            }
                                        });
                    } else {
                        addToMyPostList(postId);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    mProgressDialog.dismiss();
                }
            });
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
int a
  • 19
  • 6
  • use Image compression for the image before uploading, so that it becomes smaller size to upload or download. – Stuti Kasliwal Dec 27 '18 at 06:46
  • try these one https://github.com/zetbaitsu/Compressor , https://github.com/Tourenathan-G5organisation/SiliCompressor – Ravindra Kushwaha Dec 27 '18 at 06:47
  • 1
    Possible duplicate of [Resize an image before uploading it to firebase](https://stackoverflow.com/questions/37557343/resize-an-image-before-uploading-it-to-firebase) – GianhTran Dec 27 '18 at 06:48
  • Follow this URL https://medium.com/@adigunhammedolalekan/how-to-resize-images-for-better-upload-download-performance-android-development-fb7297f9ec24 – Mayank Sharma Dec 27 '18 at 06:50
  • mSelectedUri is this your file URI ? – Anmol Dec 27 '18 at 07:10

2 Answers2

2

You can compress the Image file using bitmap compression algorithm such as PNG compression(which is lossless compression) and then save the compressed image to a location(mNewSelectedUri) and upload to firebase.

val bitmap = MediaStore.Images.Media.getBitmap(context!!.contentResolver, mSelectedUri)
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bytes) //compress to any quality 70-90 is preferred
//new Image File
val imageFile = File(docDir, imageFilename)
//writing new compressed bitmap to file
FileOutputStream(imageFile).use { fo ->
            imageFile.createNewFile()
            fo.write(bytes.toByteArray())
}

NOTE: If you want to check the size of the image file u can use below method to check the file size before uploading.It will return you size of file in MB.

imageSizeInMB = imageFile.length().toDouble() / (1024 * 1024)

Reference: How to get URI from File?

Anmol
  • 8,110
  • 9
  • 38
  • 63
0

Compressor Lib Try this lib. Compressor will allow you to compress large photos into smaller sized photos with very less or negligible loss in quality of the image.

DKV
  • 1,767
  • 3
  • 28
  • 49