我是一个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上工作过)
任何帮助都将不胜感激。
谢谢。