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

我一直在创build图像的网格视图,图像出现在资产文件夹中。 在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(); } } 

我的应用程序确实从资源文件夹中读取图像,但是它不是遍历网格视图中的单元格。 网格视图中的所有单元格都具有从该组图像中选取的相同图像。 任何人都可以告诉我如何遍历单元格,仍然有不同的图像?

我有一个扩展BaseAdapter类的ImageAdapter类中的上述代码,在我的主类中,我通过以下方法将它链接到了我的gridview:

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

Saran提前感谢任何帮助

Solutions Collecting From Web of "Android – GridView中来自Assets文件夹的图像"

萨兰,下面是我用来显示图片资源文件夹中的图片。 我想这是一个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, View convertView, 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, 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; } } 

无需每次都读取所有项目。 只读取getView方法调用中给定位置的项目。 而那个时候只显示那个项目。

 BufferedInputStream buf = new BufferedInputStream(am.open(list[position]));