android提供了AssetManager来访问asset目录下的资源,
在activity中通过getAssets()获取AssetManager
常用的api如下:
1、列举路径下的资源String[] list(String path)
2、InputStream open(asset目录下的资源路径)
下面是放问asset目录下的图片的代码
package com.example.qunzheng.customerview;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
public class VisitAssetsResourceActivity extends Activity {
private ImageView imageView;
private String[] images = null;
private AssetManager assetManager;
private int curImageIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visit_assets_resource);
imageView = (ImageView) findViewById(R.id.image);
assetManager = getAssets();
try {
images = assetManager.list("");
} catch (IOException e) {
e.printStackTrace();
}
}
public void viewImage(View view) {
if (curImageIndex >= images.length) {
curImageIndex = 0;
}
/**
* 如果图片资源还没有释放,则释放该资源,防止内存溢出
*/
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
if (drawable != null && drawable.getBitmap() != null && !drawable.getBitmap().isRecycled()) {
drawable.getBitmap().recycle();
}
try {
InputStream assertFile = assetManager.open(images[curImageIndex++]);
imageView.setImageBitmap(BitmapFactory.decodeStream(assertFile));
} catch (IOException e) {
e.printStackTrace();
}
}
}
原文:http://www.cnblogs.com/zhengqun/p/4299151.html