Android 异步任务中的致命信号6(SIGABRT)
我在asynctask中压缩位图,并通过Bundle将其发送到另一个活动,结果导致崩溃。我正在代码中调用Bitmap.recycle()。有时它工作正常,这是我的Logcat输出Android 异步任务中的致命信号6(SIGABRT),android,bitmap,android-asynctask,Android,Bitmap,Android Asynctask,我在asynctask中压缩位图,并通过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
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;
}
}
}
private static void compressImage(最终位图,最终回调gbCallback){
新建异步任务(){
受保护的void onPreExecute(){
}
@凌驾
受保护位图doInBackground(无效…参数){
位图图像=GetScaleImage复制(位图);
如果(图像!=null){
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,80,byteArrayOutputStream);
字节[]byteArray=byteArrayOutputStream.toByteArray();
image=BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
返回图像;
}否则{
返回null;
}
}
@凌驾
受保护的void onPostExecute(位图){
gbCallback.call(位图);
}
}.执行(空,空,空);
私有静态位图getScaledImageCopyForUGC(位图图像){
试一试{
int height=image.getHeight();
int width=image.getWidth();
返回位图.createScaledBitmap(图像,400,(400*高度)/宽度,真);
}捕获(例外e){
如果(图像!=null){
返回位图.createScaledBitmap(图像,300,真);
}否则{
返回null;
}
}
}
位图似乎已经被回收。您不能使用回收的位图。您需要确保位图没有被回收,这是在压缩图像中传递的。现在您可以检查位图是否被回收以避免错误
if(!bitmap.isRecycled()){
//bitmap is not recycled
}else {
//bitmap is recycled, use a default placeholder
}
显示用于压缩添加的位图代码的代码@TusHarmoniruld这是否回答了您的问题?