-----------------------------------------main.java---------------------
package com.example.sd;
import java.io.IOException;
import java.io.InputStream;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
String []images = null;
AssetManager assetManager = null;
int currentImage = 0;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
assetManager = getAssets();
//获取/assets/目录下的所有文件
try {
images = assetManager.list("");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getNextOnclick(View view) {
//if发生数组越界
if(currentImage >= images.length) {
currentImage = 0;
}
//找到下一个图片文件
while(!images[currentImage].endsWith(".png")
&& !images[currentImage].endsWith(".jpg")
&& !images[currentImage].endsWith(".gif")) {
currentImage++;
//if已经发生数组越界
if(currentImage >= images.length) {
currentImage = 0;
}
}
InputStream inputStreamAssetFile = null;
try {
//打开指定资源对应的输入流
inputStreamAssetFile = assetManager.open(images[currentImage++]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
//如果图片还未回收,先强制回收该图片
if(bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
//改变ImageView显示的图片
imageView.setImageBitmap(BitmapFactory.decodeStream(inputStreamAssetFile));
}
}
。。。。。。。。。。。。。。。。。main.xml。。。。。。。。。。。。。。。。。。。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="next图片"
android:onClick="getNextOnclick"
/>
</LinearLayout>