我正在异步任务中压缩Bitmap,并通过Bundle将其发送到另一个活动,我得到了这个崩溃。我在我的代码中调用Bitmap.recycle()。有时它会正常工作,下面是我的Logcat输出。
Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getConfig() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getConfig() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.864 21779-22017/ A/Bitmap: Error, cannot access an invalid/free'd bitmap here!
2020-01-14 16:25:56.864 21779-22017/ A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 22017 (AsyncTask #5), pid 21779
这是压缩图像的代码
private static void compressImage(final Bitmap bitmap, final Callback<Bitmap> gbCallback) {
new AsyncTask<Void, Void, Bitmap>() {
protected void onPreExecute() {
}
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap image = getScaledImageCopy(bitmap);
if (image != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
return image;
} else {
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
gbCallback.call(bitmap);
}
}.execute(null, null, null);
private static Bitmap getScaledImageCopyForUGC(Bitmap image) {
try {
int height = image.getHeight();
int width = image.getWidth();
return Bitmap.createScaledBitmap(image, 400, (400 * height) / width, true);
} catch (Exception e) {
if (image != null) {
return Bitmap.createScaledBitmap(image, 300, 300, true);
} else {
return null;
}
}
}
发布于 2020-01-14 19:56:27
这个位图似乎已经被回收了。你不能使用回收的位图。您需要确保位图不会被回收,这是在compressImage
中传递的。现在,您可以检查位图是否被回收以避免错误。
if(!bitmap.isRecycled()){
//bitmap is not recycled
}else {
//bitmap is recycled, use a default placeholder
}
https://stackoverflow.com/questions/59732454
复制相似问题