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

Viewed 31370

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

15 Answers

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()
            }
        }
    }

This worked well for me in java

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

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)
            }
        }

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();
     }
}

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 ...

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!

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

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

Have you tried this?

val bitmap = ImageDecoder.createSource(contentResolver, uri)

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)
    }

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.

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)
}
if(result.resultCode == Activity.RESULT_OK && result.data != null {
binding?.ivImage?.setImageURI(result.data?.data)}

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
}

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()
    }
}
Related