Gallery shows blank when loading images from assets folder

时光毁灭记忆、已成空白 提交于 2020-01-05 04:00:18

问题


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; 

    }

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

and this is how I initiate the gallery

 Gallery g = (Gallery) findViewById(R.id.mygallery);

            /*新增几ImageAdapter并设定给Gallery对象*/
            try {
                g.setAdapter(new ImageAdapter(this,getImage()));
            } catch (IOException e) {
                Log.e("tag", "取得图片失败");
            }

the logcat print no error


回答1:


Just be sure that 'lis' contains full path name else you might have to prepend path to the filename. Having done that try the following changes

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



回答2:


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

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


来源:https://stackoverflow.com/questions/8373251/gallery-shows-blank-when-loading-images-from-assets-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!