public static Bitmap getImageFromAssetsFile(Resources res,String fileName)
{
Bitmap image = null;
AssetManager am = res.getAssets();
InputStream is = null;
try
{
is=am.open(fileName);
image = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
} finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return image;
} 这是我加载图片的代码。
我一次加载了几十张图片,就报01-04 01:50:12.964: ERROR/AndroidRuntime(325): java.lang.OutOfMemoryError: bitmap size exceeds VM budget网上搜了很多方法都得不到解决。求解决办法或思路。
求优化办法或思路。
LZ Google一下java.lang.OutOfMemoryError,会收获很多,还是要自己实践的。
private void dispalyNextImage() {
// 如果发生数组越界
if (currentImg >= images.length) {
currentImg = 0;
}
//备注1
// 找到下一个图片文件
while (!images[currentImg].endsWith(".png")
&& !images[currentImg].endsWith(".jpg")
&& !images[currentImg].endsWith(".gif")) {
currentImg++;
// 如果已发生数组越界
if (currentImg >= images.length) {
currentImg = 0;
}
} InputStream assetFile = null;
try {
// 打开指定资源对应的输入流
assetFile = assets.open(images[currentImg++]);
} catch (IOException e) {
e.printStackTrace();
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
//备注2
// 如果图片还未回收,先强制回收该图片
if (bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()){
bitmapDrawable.getBitmap().recycle();
}
// 改变ImageView显示的图片
image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
}提供代码吧,更直接一些!!