java - Android:Bitmap_NJI - 无法创建 SkBitmap

标签 java android

所以我有一个使用decodeFile加载图像的函数 - 然后我调整照片大小并将其绘制到 Canvas 中。这在我的 Moto Droid 2.2.3 和 Nexus 4.1.1 上运行得非常好 - 但在我的 HTC 4.0.3 上,每次 createScaledBitmap 触发时,它都会崩溃并抛出错误“无法创建 SkBitmap”...

这是代码示例...

// create image bitmap
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inPurgeable = true;
            bmOptions.inSampleSize = 2;
            Bitmap rawImage = BitmapFactory.decodeFile(imageURL,bmOptions);
            Bitmap bmp = Bitmap.createBitmap(rawImage);

            float ratio = 0;
            int maxHeight = 900;
            int maxWidth = 900;
            float height = bmp.getHeight();
            float width = bmp.getWidth();

            Log.d("HEIGHT",""+height);
            Log.d("WIDTH",""+width);
            int nh = 0;
            int nw = 0;
            // check if current width is larger
            if(width > maxWidth){
                ratio = maxWidth / width;
                float newWidth = maxWidth;
                float newHeight = height*ratio;

                nw = Math.round(newWidth);
                nh = Math.round(newHeight);

                Log.d("NEW RATIO",""+ratio);
                Log.d("NEW HEIGHT",""+newHeight);

                // RESET HEIGHT AND WIDTH
                height = height * ratio;
                width = width * ratio;
            }
            if(height > maxHeight){
                ratio = maxHeight / height;

                float newWidth = width*ratio;
                float newHeight = maxHeight;

                nw = Math.round(newWidth);
                nh = Math.round(newHeight);


                Log.d("NEW RATIO",""+ratio);
                Log.d("NEW HEIGHT",""+newHeight);
                height = height * ratio;
                width = width * ratio;
            }
            bmp = Bitmap.createScaledBitmap(bmp,nw,nh,false);
            Bitmap img = Bitmap.createBitmap(bmp,0,0,655,655);

就像我说的,这在我的其他设备和 Android 版本上运行良好...我尝试搜索,但似乎找不到任何真正解决此问题的内容...

我知道它是 createScaledBitmap,因为如果我评论该行,一切都会正常 - 但我的位图无法缩放。

最佳答案

回答这个问题 -

真正的问题是,在该特定设备上,调整大小比率太大,因此图像稍微太小,而不是适合定义的 655 区域 - 这导致调整大小错误,但在运行 4.0.3 的 HTC 设备上抛出“无法创建 SkBitmap”错误。

解决方案是调整调整后的图像的尺寸。

关于java - Android:Bitmap_NJI - 无法创建 SkBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12305870/

相关文章:

android - 如何最小化所有打开的应用程序并显示主屏幕?

java - 在 AuthenticationProvider 中使用 Autowired 时出错

java - Spring DI,工厂服务

java - 更改 Java 中的键盘输入语言

android - 如何在android Fragment 中设置自定义ListView?

android - build.xml version.tag for system tools 14,语法?

Android - 为 ListView 填充适配器的异步任务

java - Java 中的 public、protected、package-private 和 private 有什么区别?

java - 全局 servlet 页面查看计数器,这是线程安全的吗?它会产生准确的结果吗?

android - 如何在android中旋转轮子(图像png)