我正在使用 AssetManager 从 assets/image 加载图像并且加载过程很顺利,因为我可以看到所有图像都已加载到图库中,但是图库只显示框架而不显示图片,我尝试了不同的 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;
}
这是我的图像适配器
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 打印没有错误
最佳答案
只需确保“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;
}
关于android - 从 Assets 文件夹加载图像时图库显示空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373251/