方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream) [英] method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)
问题描述
我的代码中出现此错误,有人通知我如何解决?谢谢所有能提供帮助的人.
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)
线路错误
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream)
代码
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代码
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()
}
}
推荐答案
我敢打赌MediaStore.Images.Media.getBitmap
无法正确获取位图并返回null.
I bet that MediaStore.Images.Media.getBitmap
does not get the bitmap properly and returns null.
因此,如果您删除了MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?
中的?
,您将得到一个运行时异常,正如您先前的问题所断言的那样.
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.
因此将演员表更改为:
MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
为确保您永远不会获得空位图,请返回以调查getBitmap
方法
To make sure you never get a null bitmap then com back to investigate getBitmap
method
我不知道createImageFile
方法的作用,但我建议您简单地执行以下操作以测试其是否有效:
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));
最后我建议不要使用MediaStore.Images.Media.getBitmap
.
Finally I advice against of using of MediaStore.Images.Media.getBitmap
at all.
请参见已弃用.切换到图片加载库,或使用 ImageDecoder#createSource(ContentResolver,Uri)
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.
这篇关于方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!