How to reduce the size of image before uploading it to firebase storage?

后端 未结 3 1664
说谎
说谎 2020-12-13 16:30

I have made an social app for image and video sharing.However,its taking too much time for loading the image.I am using the glide library.Please, tell me how can I reduce th

3条回答
  •  天涯浪人
    2020-12-13 17:12

    StorageReference childRef2 = [your firebase storage path]
    storageRef.child(UserDetails.username+"profilepic.jpg");
                        Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
                        byte[] data = baos.toByteArray();
                        //uploading the image
                        UploadTask uploadTask2 = childRef2.putBytes(data);
                        uploadTask2.addOnSuccessListener(new OnSuccessListener() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                Toast.makeText(Profilepic.this, "Upload successful", Toast.LENGTH_LONG).show();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(Profilepic.this, "Upload Failed -> " + e, Toast.LENGTH_LONG).show();
                            }
                        });`
    

    just go ahead and do above steps it will reduce your image size and upload it on firebase it reduce the image size upto 1 to 2 mb like on my experience 4mb file became 304kb .

    filepath is and File object of your selected image. :)

提交回复
热议问题