80,351
社区成员
发帖
与我相关
我的任务
分享
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;
}
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));
}
}
提供代码吧,更直接一些!!