【问题标题】:Compress image before uploading to Firebase在上传到 Firebase 之前压缩图像
【发布时间】:2021-01-03 04:31:39
【问题描述】:

在我使用 MVVM 架构和 Kotlin 之前,我在 Java 中这样做过。

我正在将图像 Uri 传递给位图

Java 代码:

Bitmap actualImage1 = BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));

ByteArrayOutputStream baos = new ByteArrayOutputStream();
actualImage1.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] finalImage = baos.toByteArray();

getContentResolver() 无法被 Kotlin 识别。

如何在 Kotlin 中编写此代码,我再次拥有想要传递给 Bitmap 的图像 uri

【问题讨论】:

  • getContentResolver() 如何不解析它的ContextWrapper 方法,它应该在 Activity Directly 中可用。或者除了 Activity 你必须有 Context 才能像 context.getContentResolver() 一样使用它。
  • 如何在 ViewModel 中使用?还是我应该压缩 Fragment 中的图像,然后将其传递给 ViewModel?
  • getContentResolver() 作为方法参数传递到ViewModel 中,也可以在后台线程中执行此任务。您可以使用 kotlin 协程轻松实现线程..

标签: java android kotlin firebase-storage image-compression


【解决方案1】:

我在 Kotlin 中就是这样做的

   val bitmap = MediaStore.Images.Media.getBitmap(
                            this.contentResolver,
                            selectedPhotoUri
                        )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-06
    • 2023-03-19
    • 2017-03-15
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多