'Deprecated "getBitmap" with API 29. Any alternative codes?

My onActivityResult is not working because getBitmap is deprecated, any alternative codes to achieve this?

here are the codes that needs to be changed, any suggestions?

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

the getBitmap is crossed and says its deprecated



Solution 1:[1]

This worked for me,

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if(requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {

            val selectedPhotoUri = data.data
            try {
                selectedPhotoUri?.let {
                    if(Build.VERSION.SDK_INT < 28) {
                        val bitmap = MediaStore.Images.Media.getBitmap(
                            this.contentResolver,
                            selectedPhotoUri
                        )
                        imageView.setImageBitmap(bitmap)
                    } else {
                        val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
                        val bitmap = ImageDecoder.decodeBitmap(source)
                        imageView.setImageBitmap(bitmap)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

Solution 2:[2]

This worked well for me in java

ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), pictureUri);
Bitmap bitmap = ImageDecoder.decodeBitmap(source);

Solution 3:[3]

You can use:

    private fun getCapturedImage(selectedPhotoUri: Uri): Bitmap {
        val bitmap = when {
            Build.VERSION.SDK_INT < 28 -> MediaStore.Images.Media.getBitmap(
                this.contentResolver,
                selectedPhotoUri
            )
            else -> {
                val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
                ImageDecoder.decodeBitmap(source)
            }
        }

Solution 4:[4]

Check the official doc:

This method was deprecated in API level 29. loading of images should be performed through ImageDecoder#createSource(ContentResolver, Uri), which offers modern features like PostProcessor.

Solution 5:[5]

You can use this code for creating bitmap

Bitmap bitmap;
if (Build.VERSION.SDK_INT >= 29) {
     ImageDecoder.Source source = ImageDecoder.createSource(getApplicationContext().getContentResolver(), imageUri);
     try {
         bitmap = ImageDecoder.decodeBitmap(source);
     } catch (IOException e) {
         e.printStackTrace();
     }
} else {
     try {
     bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), imageUri);
     } catch (IOException e) {
          e.printStackTrace();
     }
}

Solution 6:[6]

I have created a class for loading a Bitmap from uri:

public class BitmapResolver {
    private final static String TAG = "BitmapResolver";

    @SuppressWarnings("deprecation")
    private static Bitmap getBitmapLegacy(@NonNull ContentResolver contentResolver, @NonNull Uri fileUri){
        Bitmap bitmap = null;

        try {
            bitmap = MediaStore.Images.Media.getBitmap(contentResolver, fileUri);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    @TargetApi(Build.VERSION_CODES.P)
    private static Bitmap getBitmapImageDecoder(@NonNull ContentResolver contentResolver, @NonNull Uri fileUri){
        Bitmap bitmap = null;

        try {
            bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(contentResolver, fileUri));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    public static Bitmap getBitmap(@NonNull ContentResolver contentResolver, Uri fileUri){
        if (fileUri == null){
            Log.i(TAG, "returning null because URI was null");
            return null;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
            return getBitmapImageDecoder(contentResolver, fileUri);
        } else{
            return getBitmapLegacy(contentResolver, fileUri);
        }
    }
   }

Just to save you some time ...

Solution 7:[7]

It was evident that the getBitmap API doesn't work with the latest Android SDK - 29. So, this worked for me

Uri contentURI = data.getData();
try {
    imageView.setImageURI(contentURI);
} catch (Exception e) {
    e.printStackTrace();
}

Please let me know if this doesn't work for any of you, shall other options!

Solution 8:[8]

ImageDecoder.createSource(this.getContentResolver(), pictureUri)

works fine, but to be able to use this code, mindSdkVersion should be at least 28.

Solution 9:[9]

Have you tried this?

val bitmap = ImageDecoder.createSource(contentResolver, uri)

Solution 10:[10]

hi freind you check api device

var Image_select: String? = null
var bitmap:Bitmap?=null

you show image set

binding?.ImAvator?.setImageURI(data!!.data)


 try {
                    val uri: Uri? = data!!.data
                    bitmap = if(Build.VERSION.SDK_INT>=29){
                        val source: ImageDecoder.Source = ImageDecoder.createSource(requireActivity()
                            .contentResolver, uri!!)
                        ImageDecoder.decodeBitmap(source)
                    } else{
                        MediaStore.Images.Media.getBitmap(requireActivity().contentResolver, uri!!)
                    }
               

                } catch (e: IOException) {
                    e.printStackTrace()
                }

when upload image

compress bitmap send server

 fun Camparse() {
        val size = (bitmap!!.height * (812.0 / bitmap!!.width)).toInt()
        val b = Bitmap.createScaledBitmap(bitmap!!, 812, size, true)
        val by = ByteArrayOutputStream()
        b.compress(Bitmap.CompressFormat.JPEG, 100, by)
        val bytes = by.toByteArray()
        Image_select = Base64.encodeToString(bytes, 0)
    }

Solution 11:[11]

For deprecated MediaStore.Images.Media.getBitmap() in API level 29, You can use this code:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK) {
        if (requestCode == GALLERY_REQUEST) {
            Uri selectedImage = data.getData();
            try {
                if (Build.VERSION.SDK_INT < 29) {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                    imageView2.setImageBitmap(bitmap);
                } else {
                    ImageDecoder.Source source = ImageDecoder.createSource(getActivity().getContentResolver(), selectedImage);
                    Bitmap bitmap = ImageDecoder.decodeBitmap(source);
                    imageView2.setImageBitmap(bitmap);
                }
            } catch (IOException e) {
                Toast.makeText(getContext(), R.string.error_read_image, Toast.LENGTH_LONG).show();
            }
        }
    }
}

Regards.

Solution 12:[12]

Try using ImageDecoder

bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    val src:ImageDecoder.Source = ImageDecoder.createSource(contentResolver, selectedPhotoUri!!)
    ImageDecoder.decodeBitmap(src)
}
else{
    @Suppress("DEPRECATION")
    MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
}

Solution 13:[13]

if(result.resultCode == Activity.RESULT_OK && result.data != null {
binding?.ivImage?.setImageURI(result.data?.data)}

Solution 14:[14]

For anyone getting unsupported bitmap configuration : "Hardware" error or you need mutable bitmap for Canvas or reading pixels use this

ImageDecoder.decodeBitmap(
    ImageDecoder.createSource(context.contentResolver, uri)
) { decoder, info, source ->
    decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
    decoder.isMutableRequired = true
}

Solution 15:[15]

This code works for my case:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    try {
            when (requestCode) {

                //get the image with camera
                SECTIONCAMERA -> {
                    val imageBitmap = data?.extras?.get("data") as Bitmap
                  ImageView_imagePerfil.setImageBitmap(imageBitmap)

                }
                //get the image in gallery
                SECTIONGALLERY -> {
                    val imageUri = data?.data
                    ImageView_imagePerfil.setImageURI(imageUri) }
            }

    } catch (e: Exception){
             e.printStackTrace()
    }
}