我正在使用AssetManager从资源/图像加载图像,加载过程顺利,因为我可以看到所有图像都已加载到库中,但画廊只显示框架而不是图片,我尝试了不同的mime类型png,jpg和bmp并没有成功。
这是我从assets / image
加载图像的方法private List<String> getImage() throws IOException
{
AssetManager assetManager = getAssets();
String[] files = assetManager.list("image");
List<String> it=Arrays.asList(files);
return it;
}
这是我的imageadapter
public class ImageAdapter extends BaseAdapter
{
/*声明变量*/
private Context mContext;
private List<String> lis;
/*ImageAdapter的构造符*/
public ImageAdapter(Context c,List<String> li)
{
mContext = c;
lis=li;
}
/*几定要重写的方法getCount,传回图片数目*/
public int getCount()
{
return lis.size();
}
/*一定要重写的方法getItem,传回position*/
public Object getItem(int position)
{
return position;
}
/*一定要重写的方法getItemId,传并position*/
public long getItemId(int position)
{
return position;
}
/*几定要重写的方法getView,传并几View对象*/
public View getView(int position, View convertView,
ViewGroup parent)
{
/*产生ImageView对象*/
ImageView i = new ImageView(mContext);
/*设定图片给imageView对象*/
Bitmap bm = BitmapFactory.decodeFile(lis.
get(position).toString());
i.setImageBitmap(bm);
/*重新设定图片的宽高*/
i.setScaleType(ImageView.ScaleType.FIT_XY);
/*重新设定Layout的宽高*/
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
}
}
这就是我发布图库的方式
Gallery g = (Gallery) findViewById(R.id.mygallery);
/*新增几ImageAdapter并设定给Gallery对象*/
try {
g.setAdapter(new ImageAdapter(this,getImage()));
} catch (IOException e) {
Log.e("tag", "取得图片失败");
}
logcat打印没有错误
答案 0 :(得分:1)
请确保'lis'包含完整路径名,否则您可能必须在文件名前添加路径。完成后尝试以下更改
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
ImageView i = new ImageView(mContext);
}
else {
ImageView i = (ImageView) convertView;
}
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Bitmap bm = BitmapFactory.decodeFile(lis.
get(position).toString());
i.setImageBitmap(bm);
return i;
}
答案 1 :(得分:1)
尝试这样做......
private List<String> getImage() throws IOException {
AssetManager assetManager = getAssets();
String[] files = assetManager.list("image");
List<String> list = new ArrayList<String>();
for (String it : files) {
list.add("image" + File.separator + it);
}
return list;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<String> list;
public ImageAdapter(Context c, List<String> list) {
mContext = c;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = null;
try {
if (convertView == null) {
convertView = new ImageView(mContext);
((ImageView) convertView).setAdjustViewBounds(true);
convertView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
convertView.setBackgroundResource(R.drawable.picture_frame);
}
iv = (ImageView) convertView;
InputStream is = getAssets().open(list.get(position));
Bitmap bm = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bm);
} catch (Throwable ex) {
}
return iv;
}
}
现在进入你的onCreate方法
private Gallery galery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
galery = (Gallery) findViewById(R.id.gallery);
try {
galery.setAdapter(new ImageAdapter(this, getImage()));
} catch (IOException e) {
e.printStackTrace();
}
}