方法 '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)
我的代码中有这个错误,有人告诉我如何解决吗?感谢所有能提供帮助的人。
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 代码
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)
}
}
图库中 select 图片的方法。这只是一个改编
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
没有正确获取位图并且 returns 为空。
因此,如果您删除 MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?
中的 ?
,您将得到一个运行时异常,正如您之前的问题所断言的那样。
因此将演员表更改为:
MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
为了确保您永远不会得到空位图然后回来研究getBitmap
方法
我不知道 createImageFile
方法有什么作用,但我建议您简单地执行以下操作来测试它是否有效:
MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse("file://"+uri));
最后我建议完全不要使用 MediaStore.Images.Media.getBitmap
。
参见 Its deprecated. switch to image loading libraries or use ImageDecoder#createSource(ContentResolver, Uri)
上述link中有指南。喜欢这个:
public static Bitmap decodeBitmap (ImageDecoder.Source src)
还要确保在工作线程中执行这些操作。
我的代码中有这个错误,有人告诉我如何解决吗?感谢所有能提供帮助的人。
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 代码
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)
}
}
图库中 select 图片的方法。这只是一个改编
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
没有正确获取位图并且 returns 为空。
因此,如果您删除 MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?
中的 ?
,您将得到一个运行时异常,正如您之前的问题所断言的那样。
因此将演员表更改为:
MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
为了确保您永远不会得到空位图然后回来研究getBitmap
方法
我不知道 createImageFile
方法有什么作用,但我建议您简单地执行以下操作来测试它是否有效:
MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse("file://"+uri));
最后我建议完全不要使用 MediaStore.Images.Media.getBitmap
。
参见 Its deprecated. switch to image loading libraries or use ImageDecoder#createSource(ContentResolver, Uri)
上述link中有指南。喜欢这个:
public static Bitmap decodeBitmap (ImageDecoder.Source src)
还要确保在工作线程中执行这些操作。