1

I'm trying to use TensorImage.load() to load a bitmap of picture the user took with the camera app. When I pass in the bitmap I get this error: java.lang.IllegalArgumentException: Only supports loading ARGB_8888 bitmaps

This is my code for when I call the load function. First, it starts with the onActivityResult:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == CAMERA_REQUEST_CODE) {
        if(Build.VERSION.SDK_INT < 28) {
            val foodBitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, Uri.fromFile(photoFile))
            // pass this bitmap to the classifier
            val predictions = foodClassifier.recognizeImage(foodBitmap, 0)
        } else {
            val source = ImageDecoder.createSource(this.contentResolver, Uri.fromFile(photoFile))
            val foodBitmap = ImageDecoder.decodeBitmap(source)
            // pass this bitmap to the classifier
            val predictions = foodClassifier.recognizeImage(foodBitmap, 0)
        }
    }
}

In the recognizeImage function, I call a variable named inputImageBuffer which is of type TensorImage. I call the load function and pass the bitmap. This is where the application crashes. Can someone tell me how do I fix this?

Bert Hanz
  • 417
  • 1
  • 7
  • 16

2 Answers2

2

I solved the issue by changing the bitmap configuration in simplest way.

Bitmap bmp = imageBitmap.copy(Bitmap.Config.ARGB_8888,true) ;

Here ii - Bitmap is immutable therefore I have make a copy with Bitmap.Config.ARGB_8888 configuration and a new Bitmap with refrence, for further reference
https://developer.android.com/reference/android/graphics/Bitmap.Config

0

For anyone else this is how I solved the issue by changing the bitmap configuration.

// Convert the image to a Bitmap
        var bitmap: Bitmap? = null
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                val source = ImageDecoder.createSource(requireContext().contentResolver, uri!!)
                bitmap = ImageDecoder.decodeBitmap(source)
                bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
            } else {
                bitmap = MediaStore.Images.Media.getBitmap(requireContext().contentResolver, uri!!)
            }
        } catch (e: Exception) {
            println("Could not convert image to BitMap")
            e.printStackTrace()
        }
Andrew Olson
  • 486
  • 1
  • 5
  • 4