Android Bitmap.decodeStream返回null

时间:2011-12-27 10:41:29

标签: android bitmap decode

我正在尝试在我的应用程序中显示图像,但在某些情况下我面对OutOfMemory Exception我决定使用inJustDecodeBounds来缩放位图。但是当我使用这段代码时,decodeStream会返回null。 有什么想法吗?

File bufferFile = new File(ids.get(position));
        FileInputStream fis   = new FileInputStream(bufferFile);

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        CipherInputStream cis = new CipherInputStream(fis, cipher);


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

        BitmapFactory.decodeStream(cis, null, options);

        Boolean scaleByHeight = Math.abs(options.outHeight - Cards.height) >= Math.abs(options.outWidth - Cards.width);

        if(options.outHeight * options.outWidth * 2 >= 200*200*2){
               // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
              double sampleSize = scaleByHeight
                    ? options.outHeight / Cards.height
                    : options.outWidth / Cards.width;
              options.inSampleSize = 
                    (int)Math.pow(2d, Math.floor(
                    Math.log(sampleSize)/Math.log(2d)));
           }

              // Do the actual decoding
              options.inJustDecodeBounds = false;

              //cis.close();
              cis = new CipherInputStream(fis, cipher);
              Bitmap img = BitmapFactory.decodeStream(cis, null, options);
              cis.close();


       ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(img);

        fis.close();


        Runtime.getRuntime().gc();

1 个答案:

答案 0 :(得分:0)

也许this是你的问题? bug报告还包含一个可以尝试的解决方法。