Nexus 7,Android 4.4和png图像

4

我是一个Android的初学者,现在有一个问题需要解决。我正在创建一个应用程序,需要从Assets文件夹加载图像。这些图像是带透明背景的PNG格式。我正在使用BitmapFactory进行处理,Google网站上建议的方式是这样的=>

   public Bitmap getBitmap( Activity activity, String fileName) {           
            try
            {
                int reqWidth =getCalculatedWidth();
                int reqHeight=getCalculatedHeight();
                AssetManager asManager =activity.getAssets();
                InputStream istr = asManager.open(fileName);
                // First decode with inJustDecodeBounds=true to check dimensions
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;

                BitmapFactory.decodeStream(istr, null, options);

                options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

                // Decode bitmap with inSampleSize set
                options.inJustDecodeBounds = false;
                Bitmap bmp =BitmapFactory.decodeStream(istr,null, options);
                Toast.makeText(activity, fileName+" was successfuly loaded", Toast.LENGTH_SHORT).show();
                return   bmp;


            }
            catch (IOException ex)
            {
                Toast.makeText(activity, ex.getMessage(), Toast.LENGTH_LONG).show();
                return null;
            }

        }

private int calculateInSampleSize(
            BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }

        }

        return inSampleSize;
    }

我遇到的问题是这样的。一切都按预期工作,但昨天我安装了Android 4.4,当我启动应用程序时,没有加载任何图像。我试图进行研究,但与我的问题无关,它根本不加载PNG图像。我将它们转换为GIF,图像被加载了,但透明部分变成了白色背景。
我找到了这篇文章:https://code.google.com/p/android/issues/detail?id=62016 ,解决方法对我也起作用,但我不想使用GIF图像,我必须使用PNG。GIF质量不令人满意。
请注意,Toast“文件已成功加载”会显示,因此没有抛出异常。
问题可能是什么,这只发生在我身上吗?
我测试的设备是:Nexus 7 2012(Wifi)而非移动设备。我曾经有过4.3,昨天我将其更新为4.4(作为常规系统更新)。
请注意,在其他设备上仍然像以前一样正常,在模拟器上也正常。(我测试的其他设备上没有4.4,而是旧版本)。我只有在nexus 7上有4.4,它是唯一无法工作的地方。(在4.3上工作过)
任何帮助都将不胜感激。
谢谢。
2个回答

4

我在相同的情况下遇到了同样的问题。我通过调用inputStream.reset()解决了这个问题,以便再次使用inputStream,否则BitmapFactory.decodeStream将返回null

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeStream(istr, null, options);

try {
    istr.reset();
} catch (IOException e) {
    Log.e(TAG, e.getMessage());
}

options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeStream(istr,null, options);

我简直不敢相信解决方案如此简单。我花了几天时间重新实现另一个解决方案,但并不像这个好。非常感谢您。 - aleksandaril
谁会想到...很遗憾,这种(我指的是无需重置解码)方法一直有效,直到4.4版本。 - Alex Semeniuk
istr.reset() 对我抛出了 IOexception 异常,你有什么想法是什么情况导致的? - Vrashabh Irde
@Slartibartfast 你是否正在使用一个 markSupported() 返回 false 的 InputStream?例如,可能是来自 ContentResolver? - Mark
经过很长时间,提出这个问题的应用程序可以在这里找到。@TGomews 再次感谢您的回答。 - aleksandaril

0

对于在Android KitKat 4.4中遇到问题的其他人,以下内容可能会有所帮助:

Bitmap b = BitmapFactory.decodeStream(new BufferedInputStream(is)); imageView.setImageBitmap(b);

我在更新到4.4后在Nexus系列中遇到了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接