importjava.io.IOException;importjava.io.InputStream;importandroid.app.Activity;importandroid.content.res.AssetManager;importandroid.graphics.BitmapFactory;importandroid.graphics.drawable.BitmapDrawable;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.ImageView;public class MainActivity extendsActivity {
String[] images= null;
AssetManager assets= null;int currentImge = 0;
ImageView image;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView) findViewById(R.id.imageBitmap);try{
assets=getAssets();//获取/assests/目录下的所有的文件
images = assets.list("");
}catch(IOException e) {
e.printStackTrace();
}final Button next =(Button) findViewById(R.id.btnBitmap);
next.setOnClickListener(newOnClickListener() {
@Overridepublic voidonClick(View v) {//TODO Auto-generated method stub
if (currentImge >=images.length) {
currentImge= 0;
}//找到下一个图片文件,这里使用jpg图片
while (!images[currentImge].endsWith(".jpg")) {
currentImge++;//如果发生数组越界
if (currentImge >=images.length) {
currentImge= 0;
}
}
InputStream assetFile= null;try{//打开指定资源对应的输入流
assetFile = assets.open(images[currentImge++]);
}catch(IOException e) {
e.printStackTrace();
}
BitmapDrawable bitmapDrawable=(BitmapDrawable) image
.getDrawable();//如果图片还未回收,先强制回收该图片
if (bitmapDrawable != null
&& !bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}//该变现实的图片
image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
});
}
}