Android 从“资源”文件夹加载图像时,图库显示为空白
我正在使用AssetManager从assets/image加载图像,加载过程进行得很顺利,因为我可以看到所有图像都已加载到库中,但库中只显示帧,而不显示图片,我尝试了不同的mime类型png、jpg和bmp,但没有成功 这是我从资产/图像加载图像的方法Android 从“资源”文件夹加载图像时,图库显示为空白,android,gallery,assets,Android,Gallery,Assets,我正在使用AssetManager从assets/image加载图像,加载过程进行得很顺利,因为我可以看到所有图像都已加载到库中,但库中只显示帧,而不显示图片,我尝试了不同的mime类型png、jpg和bmp,但没有成功 这是我从资产/图像加载图像的方法 private List<String> getImage() throws IOException { AssetManager assetManager = getAssets(); String[] fil
private List<String> getImage() throws IOException
{
AssetManager assetManager = getAssets();
String[] files = assetManager.list("image");
List<String> it=Arrays.asList(files);
return it;
}
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;
}
试着这样做
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;
}
}
视图不是空的,图像在那里,我可以看到框架或单击它,但图片只是变成黑色
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;
}
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;
}
}
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();
}
}