android - Bitmap.copy 在 createScaledBitmap 工作的地方失败

标签 android bitmap crash copy

我在使用 Bitmap.copy 函数时遇到问题。这段代码工作正常,

Bitmap tempBM = Bitmap.createScaledBitmap(sourceBitmap, sourceBitmap.getWidth(), sourceBitmap.getHeight(), false);

//Ensure that the bitmap is mutable and not copied from the original in the case where no scaling is required
m_bwBitmap = tempBM.copy(tempBM.getConfig(), true);
if (tempBM!=sourceBitmap)
{
    tempBM.recycle();
}

但这不...

m_bwBitmap = sourceBitmap.copy(sourceBitmap.getConfig(), true);

sourceBitmap 一开始是不可变的,我希望 m_bwBitmap 是可变的。

它不会像这样崩溃,但它确实会破坏调试器,就好像 android 函数某处出了问题一样。该应用程序稍后会崩溃。如果我用顶级代码替换它,一切正常。

但是,我现在已经开始从 JellyBean 获取崩溃报告,在带有 tempBM.copy 的行上抛出一个空指针异常。所以,我必须解决这个问题,但目前顶级代码是唯一可行的来源。我正在 Android 4.0 设备上对其进行测试。

有什么想法吗?

最佳答案

好的,我想我已经回答了这个问题(无论如何至少回答了一半)。

这与 Bitmap.Config 有关。如果我将行更改为

m_bwBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);

然后它工作正常。

请注意,原始源位图来自这样一行...

Bitmap sourceBitmap = BitmapFactory.decodeFile(pictureFile);

pictureFile 是一个 GIF。

但是,我真的不知道为什么 decodeFile 会生成一些看似无效的配置。如果我检查 sourceBitmap 的配置,它返回 null ?!?

关于android - Bitmap.copy 在 createScaledBitmap 工作的地方失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13260593/

相关文章:

java - Android 版谷歌地图应用程序无法在市场上使用

android - 无法在 Android Studio : Unable to load native library 中使用 CMake 链接库

android - 从位图中获取单列像素?

ios - iOS 7 崩溃 : [NSConcreteMapTable assign:key:value:isNew:]

android - 在 Unity 中影响移动设备 FPS 的因素

android - 在 values\attrs.xml 中声明属性时出错

android - java.lang.OutOfMemoryError : bitmap size exceeds VM budget on line Drawable. createFromStream(((java.io.InputStream) new URL(url), "") 在android中

android半透明位图背景为黑色

java - 使用 TextWatcher addTextChangedListener() 在 EditText 中设置文本时出现 IndexOutOfBoundsException

memory-management - 如何解决 iOS 6 上 Map 的内存问题