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

后端 未结 3 1672
说谎
说谎 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:19

    Here i am using this code to upload compressed image on firebase storeage

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode==RC_PHOTO_PICKER && resultCode==RESULT_OK)
            {
                mProgressBar.setVisibility(ProgressBar.VISIBLE);
                Uri selectedImageUri = data.getData();
    
                Bitmap bmp = null;
                try {
                    bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
                //here you can choose quality factor in third parameter(ex. i choosen 25) 
                bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
                byte[] fileInBytes = baos.toByteArray();
    
               StorageReference photoref = chatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
    
               //here i am uploading
               photoref.putBytes(fileInBytes).addOnSuccessListener(this, new OnSuccessListener() {
                           public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                              // When the image has successfully uploaded, we get its download URL
                               mProgressBar.setVisibility(ProgressBar.INVISIBLE);
    
                               Uri downloadUrl = taskSnapshot.getDownloadUrl();
                               String id = chatRoomDataBaseReference.push().getKey();
                               String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    
                               // Set the download URL to the message box, so that the user can send it to the database
                               FriendlyMessageModel friendlyMessage = new FriendlyMessageModel(id,null, userId, downloadUrl.toString(),time);
                       chatRoomDataBaseReference.child(id).setValue(friendlyMessage);
                                           }
                       });
            }
        }
    

提交回复
热议问题