首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AsyncTask安卓中的致命信号6 (SIGABRT)

AsyncTask安卓中的致命信号6 (SIGABRT)
EN

Stack Overflow用户
提问于 2020-01-14 19:00:53
回答 1查看 313关注 0票数 0

我正在异步任务中压缩Bitmap,并通过Bundle将其发送到另一个活动,我得到了这个崩溃。我在我的代码中调用Bitmap.recycle()。有时它会正常工作,下面是我的Logcat输出。

代码语言:javascript
运行
复制
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 

这是压缩图像的代码

代码语言:javascript
运行
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-14 19:56:27

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

代码语言:javascript
运行
复制
if(!bitmap.isRecycled()){
    //bitmap is not recycled
}else {
    //bitmap is recycled, use a default placeholder
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59732454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档