method 'boolean android.graphics.Bitmap.compre

2019-08-17 07:29发布

问题:

I am having this error in my code, someone informs me how to solve it? Thank you to everyone who can help.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
        at map.app.fragments.ReportFragment.putImgToBytearray(ReportFragment.kt:177)

line error

bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream)

code

private fun putImgToBytearray(): ByteArray {
    val stream = ByteArrayOutputStream()
    val drawable = this.imgThumb!!.drawable as BitmapDrawable
    val bitmap = drawable.bitmap
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream)
    return stream.toByteArray()
}

onActivityResult code

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {


        try {

            //Getting the Bitmap from Gallery
            val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?
            this.imgThumb!!.setImageBitmap(bitmap)
            this.pictureTaken = true
        } catch (e:IOException) {
            e.printStackTrace()
        }
    } else {
        Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)
    }
}

method for select image from gallery. it's just an adaptation

fun openCamera() {
    try {
        val imageFile = createImageFile()
        val callCameraIntent =  Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        if(callCameraIntent.resolveActivity(activity.packageManager) != null) {
            val authorities = activity.packageName + ".fileprovider"
            this.imageUri = FileProvider.getUriForFile(context, authorities, imageFile)
            callCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
            startActivityForResult(callCameraIntent, IMAGE_PICK_CODE)
        }
    } catch (e: IOException) {
        Toast.makeText(context, "Could not create file!", Toast.LENGTH_SHORT).show()
    }
}

回答1:

I bet that MediaStore.Images.Media.getBitmap does not get the bitmap properly and returns null.

So if you remove the ? in MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap? you will get a runtime exception as your previous question asserts.

So change the cast to:

MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap

To make sure you never get a null bitmap then com back to investigate getBitmap method

I have no idea what does the createImageFile method do, but I suggest you to simply do the following to test if it is working:

MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse("file://"+uri));

Finally I advice against of using of MediaStore.Images.Media.getBitmap at all.

See Its deprecated. switch to image loading libraries or use ImageDecoder#createSource(ContentResolver, Uri)

There are guides in the mentioned link. Like this one:

public static Bitmap decodeBitmap (ImageDecoder.Source src)

Also make sure to do these kin of stuff in a worker thread.