【问题标题】:Fatal signal 6 (SIGABRT) in AsyncTask AndroidAsyncTask Android 中的致命信号 6 (SIGABRT)
【发布时间】:2020-05-01 02:25:23
【问题描述】:

我在 asynctask 中压缩 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;
        }
    }
}

【问题讨论】:

标签: android bitmap android-asynctask


【解决方案1】:

位图似乎已经被回收了。您不能使用回收的位图。您需要确保在compressImage 中传递的位图未被回收。现在您可以检查位图是否被回收以避免错误。

if(!bitmap.isRecycled()){
    //bitmap is not recycled
}else {
    //bitmap is recycled, use a default placeholder
}

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 2016-01-05
    • 2014-04-29
    • 2014-12-21
    • 2015-03-29
    • 2017-03-07
    • 2014-06-25
    • 1970-01-01
    相关资源
    最近更新 更多