Problem on some phones when encoding a bitmap into a byte [] array

I have an icon downloaded from the application manager. Usually it is small, usually 48x48. I save this icon and load it later.

Some users report problems trying to load a saved icon. This happens for different icons for different users, and the only common denominator is that they all have phones running Android 1.5 (Sprint Hero, Sprint Moment, Droid Eris).

//Returns a valid drawable 100% of the time Drawable drawable = activityInfo.loadIcon(manager); //Creates a bitmap 100% of the time Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); //This drawable can always be displayed (so you know the bitmap is good here. Drawable testDrawable = new BitmapDrawable(bitmap); //There are no errors thrown from these lines but in ALL cases if it fails the length of the byte[] array b is 48. //When it succeeds the length is much bigger 1000+. ByteArrayOutputStream out= new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, out); byte[] b = out.toByteArray(); 

I can easily say that the user icon did not load, because you cannot create the correct raster image from an array of bytes of length 48.

What can I change in 3 lines that create an array of bytes to fix the problem? I also tried, with nochange:

bitmap.compress(CompressFormat.PNG, 100, out);

+4
source share
2 answers

I had a similar problem on Android 1.5, while Android 1.6, 2.1 and 2.2 worked fine.

In my case, bitmap.compress (Bitmap.CompressFormat.PNG, 100, outputstream) crashed only on bitmaps created from PNG images using BitmapFactory.decodeByteArray (...)

A workaround / solution to this problem was to clone such a bitmap before calling its compress (...) method, as in the code below

 boolean success = bitmap.compress(CompressFormat.PNG, 100, outputStream); if (! success) { Bitmap cloneImg = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), false); outputStream = new ByteArrayOutputStream(); cloneImg.compress(CompressFormat.PNG, 100, outputStream); } 
+2
source

You can also check the boolean returned by the compress () method to determine if compression is performed or not.

How to succeed, I do not know if there is information here for this. In particular, I am curious about the true location of OutputStream, since I believe that byteArrayOutputStream is not the actual stream used to save the icon for later use. Since they are located on 1.5 devices, I have a suspicion that you are trying to save this icon either in your directory or in the directory located on the phone. I mean, it can range from a faulty I / O device to insufficient storage space. My hunch that this happens on 1.5 devices is that there is not enough space on the physical device, since most (not all) 1.5 phones do not come with large internal storage.

Also, I don’t think that changing the compression quality in PNG format will matter since PNG will be lossless.

0
source

Source: https://habr.com/ru/post/1302392/


All Articles