Android – GridView中Assets文件夹中的图像

我一直在创建图像的网格视图,图像存在于Assets文件夹中. Opening a File from assets folder in android链接帮助我使用位图来读取它.目前的代码是:
public View getView(final int position,View convertView,ViewGroup parent) 
{

  try 
    {
     AssetManager am = mContext.getAssets();
     String list[] = am.list("");
     int count_files = imagelist.length;
     for(int i= 0;i<=count_files; i++)
     {
      BufferedInputStream buf = new BufferedInputStream(am.open(list[i]));
      Bitmap bitmap = BitmapFactory.decodeStream(buf);
      imageView.setimageBitmap(bitmap);
      buf.close();
     }
   }   
   catch (IOException e) 
   {
   e.printstacktrace();
   }
  }

我的应用程序从Assets文件夹中读取图像,但它不是遍历网格视图中的单元格.网格视图的所有单元格具有从图像集中选取的相同图像.任何人都可以告诉我如何迭代单元格,仍然有不同的图像?

我在ImageAdapter类中有上述代码,它扩展了BaseAdapter类,在我的主类中,我将其与gridview进行链接

GridView  gv =(GridView)findViewById(R.id.gridview);
    gv.setAdapter(new ImageAdapter(this,assetlist));

非常感谢任何帮助提前,
萨兰

解决方法

下面的Saran是我用来在图库的资源文件夹中显示图像的.我想像一个gridview是一样的:
public class myActivitye extends Activity
{
    private gallery mgallery;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mgallery = (gallery) findViewById(R.id.mygalleryinxml);

        //load images into memory
        mBitArray = new Bitmap[4];
        try
        {
            //these images are stored in the root of "assets"
            mBitArray[0] = getBitmapFromAsset("pic1.png");
            mBitArray[1] = getBitmapFromAsset("pic2.png");
            mBitArray[2] = getBitmapFromAsset("pic3.png");
            mBitArray[3] = getBitmapFromAsset("pic4.png");
        }
        catch (IOException e)
        {
            e.printstacktrace();
        }

        mgallery.setAdapter(new galleryAdapter(this,mBitArray));
    }

    public class galleryAdapter extends BaseAdapter
    {
        //member variables
        private Context mContext;
        private Bitmap[] mImageArray;

        //constructor
        public galleryAdapter(Context context,Bitmap[] imgArray)
        {
            mContext = context;
            mImageArray = imgArray;
        }

        public int getCount()
        {
            return mImageArray.length;
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        //returns the individual images to the widget as it requires them
        public View getView(int position,ViewGroup parent)
        {
            final ImageView imgView = new ImageView(mContext);

            imgView.setimageBitmap(mImageArray[position]);

            //put black borders around the image
            final RelativeLayout borderImg = new RelativeLayout(mContext);
            borderImg.setPadding(20,20,20);
            borderImg.setBackgroundColor(0xff000000);//black
            borderImg.addView(imgView);
            return borderImg;
        }

    }//end of: class galleryAdapter


    /**
     * Helper Functions
     * @throws IOException 
     */
    private Bitmap getBitmapFromAsset(String strName) throws IOException
    {
        AssetManager assetManager = getAssets();

        InputStream istr = assetManager.open(strName);
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        istr.close();

        return bitmap;
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...