本文介绍了从装载资产文件夹的图像时,画廊显示空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AssetManager载入图像形成的资产/图像和加载过程很顺利,因为我能看到的所有图像都在走廊被加载,但画廊只显示帧,而不是图片,我尝试了不同的MIME类型PNG,JPG和BMP和没有工作了。

I am using AssetManager to load images form assets/image and the loading process went well as I can see all images have been loaded in the gallery, but the gallery only shows the frame but not the pictures, I tried different mime types png ,jpg and bmp and didn't work out.

这是我从加载资产/图像图像的方法。

this is the method I load images from 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

This is my 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中没有错误

the logcat print no error

推荐答案

尝试做到这一点...

Try to do this...

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方法

Now in your onCreate Method

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();
        }
    }

这篇关于从装载资产文件夹的图像时,画廊显示空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:40