-1

Before I was using MVVM architecture and Kotlin I done it like this in Java.

I am passing image Uri to Bitmap

Java code:

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

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

getContentResolver() is not recognized by Kotlin.

How to write this in Kotlin, again I have image uri that I want to pass to Bitmap

Ashish Kamble
  • 2,555
  • 3
  • 21
  • 29
  • How `getContentResolver()` is not resolve its a method of `ContextWrapper` it should be available inside Activity Directly . Or other than Activity you have to have `Context` to use it like `context.getContentResolver()`. – ADM Sep 16 '20 at 10:51
  • How to use it inside ViewModel? Or should I compress the images in Fragment and then pass it to ViewModel? – Marijan Klarić Sep 16 '20 at 10:54
  • 1
    Pass the `getContentResolver()` as a method parameter inside `ViewModel` also do this task in a background thread . You can use kotlin coroutine for easy implementation for threading .. – ADM Sep 16 '20 at 10:55

1 Answers1

0

I do it like this in Kotlin

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