【发布时间】: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