Question

I am beginner with android and I have an issue. I am creating application that require loading images from assets folder. The images are png format with transparent background. I am using BitmapFactory for the purpose the way Google suggest on their website (something like this) =>

   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;
    }

The problem I have is this. Everything worked as expected, but yesterday I installed Android 4.4 and when I start the app, no images were loaded. I tried to make a research, but nothing related to my issue, It doesn't load PNG images at all. I convert them to GIF and images were loaded but instead transparent they were with white background. I found this post: https://code.google.com/p/android/issues/detail?id=62016 , and the workaround work for me too, but I don't want to use GIF images, I want png, I have to. GIF Quality is not satisfying.

Please note that the Toast "file is loaded successfully" is shown so there is no exception thrown.

What could be the problem and does this happens only to me?

Device where I test is: Nexus 7 2012 (Wifi) not mobile I had 4.3 and Yesterday I update it to 4.4 (as Regular system update).

Please note that on other devices works fine as before, on the emulator works fine as well. (On other devices I test, I don't have 4.4, but older versions). I only have 4.4 on my nexus 7, and it is the only place where it doesn't work. (It worked on 4.3)

Any help would be appreciated and welcome. Thank You

Was it helpful?

Solution

I had the same problem in the same circumstances. I solved the problem calling inputStream.reset() to use the inputStream again, otherwise BitmapFactory.decodeStream returns 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);

OTHER TIPS

Others who are facing issues in Android KitKat 4.4 , following would be helpful

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

I was facing this issue in Nexus series After updating to 4.4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top