我有一个从应用管理器加载的图标。它通常很小,通常是48x48像素。我保存这个图标并稍后重新加载。
一些用户报告说当我尝试加载保存的图标时出现问题。这对于不同用户的不同图标都会发生,唯一的共同点是他们都使用运行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();
我可以轻松地告诉你,用户图标无法加载,因为从48长度的字节数组中无法创建有效的位图。您能否在创建字节数组的三行代码中进行更改以解决问题?我也尝试过,但没有变化:
bitmap.compress(CompressFormat.PNG, 100, out);