【问题标题】:How do I reduce size/resolution of an image before uploading to firebase storage?如何在上传到 Firebase 存储之前减小图像的大小/分辨率?
【发布时间】:2018-11-22 17:52:03
【问题描述】:

我正在尝试将图像上传到 firebase 存储,但大图像使我的应用程序滞后/变慢,因此我想缩小图像或有其他替代方法

【问题讨论】:

  • 图像压缩研究。您可以将位图压缩为 JPEG 或 PNG 等。顺便说一下,请确保在单独的线程中上传(使用异步任务),因为您不想在上传时阻塞 UI 线程
  • @SURAJ BISHT,我的回答有帮助吗?我认为将位图压缩为 JPEG 应该可以解决使图像更小的问题,以便应用程序不会滞后。

标签: android firebase firebase-storage


【解决方案1】:

这是一个您可以用来执行此操作的函数。您需要将图像的bitmap 传递到函数中:

private void doUpload(Bitmap bitmap) {
    //Change this to your picture name.
    String s = "MyPicture.jpg";
    StorageReference storageReference2 = FirebaseStorage.getInstance().getReference(s);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    //Compress the original bitmap down into a JPEG.
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] data2 = baos.toByteArray();

    //Get an array of the bytes and upload it to Firebase Storage.
    UploadTask uploadTask = storageReference2.putBytes(data2);
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            makeToast("Exception " + exception.getMessage());
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            makeToast("Picture uploaded.");
        }
    });
}

这应该会将图片上传到 Firebase 存储。它将图像压缩为JPEG。您可以通过更改上面的String s 的值来更改图片的名称。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-08-19
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2019-11-06
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多