【问题标题】:How to re-size image before uploading it to Firebase?如何在将图像上传到 Firebase 之前调整其大小?
【发布时间】:2018-07-11 18:26:42
【问题描述】:

我之前看到过这个问题,但没有一个解决方案真正适合我的情况。

除了节省一些空间外,我还想节省从 Firebase 存储上传/下载内容的时间。由于firebase使用图像uri,我想不出一个合适的方法来实现这一点。

解决方案可能是创建一个“副本”到缓存或一些临时文件以调整大小、裁剪等,然后将该副本上传到存储。但是我不知道这是否是一个好方法,我真的不知道该怎么做。

您能建议一些方法吗?我真的很感激例子。

【问题讨论】:

  • 您还可以使用 Cloud Functions 触发器编写在上传完成时自动执行调整大小的代码。 github.com/firebase/functions-samples/tree/master/…
  • @DougStevenson ,我真的很喜欢这种方法,但我以前从未使用过 Cloud Functions,它是免费服务还是付费服务?我会进一步研究它,谢谢

标签: java android image firebase firebase-storage


【解决方案1】:

使用第三方库,简单高效

分级

dependencies {
    compile 'id.zelory:compressor:2.1.0'
}

压缩图像文件

compressedImageFile = new Compressor(this).compressToFile(actualImageFile);

将图像文件压缩为位图

compressedImageBitmap = new Compressor(this).compressToBitmap(actualImageFile);

欲了解更多信息,请访问此

https://github.com/zetbaitsu/Compressor

希望这会有所帮助,因为我一直使用这个库。

【讨论】:

    【解决方案2】:

    您可以使用一些定制的libraries 来压缩图像和视频等媒体文件。例如:Compressor是很好的图片压缩库:https://github.com/zetbaitsu/Compressor

    要压缩图像并在Firebase Storage 上上传,您可以简单地执行以下操作:

    try {
                    Bitmap bitmap = new Compressor(this)
                            .setMaxHeight(200) //Set height and width
                            .setMaxWidth(200)
                            .setQuality(100) // Set Quality
                            .compressToBitmap(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                final byte[] bytes = baos.toByteArray();
    

    然后在调用和设置UploadTask时:

    UploadTask uploadTask = StorageReference.putBytes(bytes);
    

    当然还有一些其他的libraries 喜欢:

    SiliCompressor

    https://github.com/Tourenathan-G5organisation/SiliCompressor

    【讨论】:

    • 感谢您的回答,我尝试了您的代码,但我得到了这个:FileNotFoundException: /primary:Pictures/night_sky_moon_trees_river_reflection_95979_1920x1080.jpg (No such file or directory) 这是文件:new File(data.getClipData().getItemAt(i).getUri().getLastPathSegment()); 这是来自 ImageChooser
    • 这是另一个exception,当您尝试读取文件时,文件不存在或您的读取设置不正确。
    • 是的,我知道异常意味着什么,但是如果直接来自 ImageChooser,那么 uri 怎么会出错呢?同样的 uri 在上述课程中也有效。谢谢
    【解决方案3】:

    你可以使用这个库

    分级

    dependencies {
            implementation 'com.github.WindSekirun:MediaResizer:1.0.0' 
    }
    
    allprojects {
        repositories {
         maven { url 'https://jitpack.io' }
        }
    

    压缩图片

    ImageResizeOption resizeOption = new ImageResizeOption.Builder()
                .setImageProcessMode(ImageMode.ResizeAndCompress)
                .setImageResolution(1280, 720)
                .setBitmapFilter(false)
                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                .setCompressQuality(75)
                .setScanRequest(ScanRequest.TRUE)
                .build();
    
    ResizeOption option = new ResizeOption.Builder()
                .setMediaType(MediaType.IMAGE)
                .setImageResizeOption(resizeOption)
                .setTargetPath(path)
                .setOutputPath(imageFile.getAbsolutePath())
                .setCallback((code, output) -> {
                    txtStatus.setText(ResultBuilder.displayImageResult(code, path, output));
                    progress.dismiss();
                }).build();
    
    MediaResizer.process(option);
    

    【讨论】:

      猜你喜欢
      • 2016-09-30
      • 2021-11-14
      • 2018-03-14
      • 1970-01-01
      • 2015-05-02
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      相关资源
      最近更新 更多