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

前端之家收集整理的这篇文章主要介绍了Android – GridView中Assets文件夹中的图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在创建图像的网格视图,图像存在于Assets文件夹中. Opening a File from assets folder in android链接帮助我使用位图来读取它.目前的代码是:
  1. public View getView(final int position,View convertView,ViewGroup parent)
  2. {
  3.  
  4. try
  5. {
  6. AssetManager am = mContext.getAssets();
  7. String list[] = am.list("");
  8. int count_files = imagelist.length;
  9. for(int i= 0;i<=count_files; i++)
  10. {
  11. BufferedInputStream buf = new BufferedInputStream(am.open(list[i]));
  12. Bitmap bitmap = BitmapFactory.decodeStream(buf);
  13. imageView.setImageBitmap(bitmap);
  14. buf.close();
  15. }
  16. }
  17. catch (IOException e)
  18. {
  19. e.printStackTrace();
  20. }
  21. }

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

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

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

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

解决方法

下面的Saran是我用来在图库的资源文件夹中显示图像的.我想像一个gridview是一样的:
  1. public class myActivitye extends Activity
  2. {
  3. private Gallery mGallery;
  4.  
  5. @Override
  6. public void onCreate(Bundle savedInstanceState)
  7. {
  8.  
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11.  
  12. mGallery = (Gallery) findViewById(R.id.mygalleryinxml);
  13.  
  14. //load images into memory
  15. mBitArray = new Bitmap[4];
  16. try
  17. {
  18. //these images are stored in the root of "assets"
  19. mBitArray[0] = getBitmapFromAsset("pic1.png");
  20. mBitArray[1] = getBitmapFromAsset("pic2.png");
  21. mBitArray[2] = getBitmapFromAsset("pic3.png");
  22. mBitArray[3] = getBitmapFromAsset("pic4.png");
  23. }
  24. catch (IOException e)
  25. {
  26. e.printStackTrace();
  27. }
  28.  
  29. mGallery.setAdapter(new GalleryAdapter(this,mBitArray));
  30. }
  31.  
  32. public class GalleryAdapter extends BaseAdapter
  33. {
  34. //member variables
  35. private Context mContext;
  36. private Bitmap[] mImageArray;
  37.  
  38. //constructor
  39. public GalleryAdapter(Context context,Bitmap[] imgArray)
  40. {
  41. mContext = context;
  42. mImageArray = imgArray;
  43. }
  44.  
  45. public int getCount()
  46. {
  47. return mImageArray.length;
  48. }
  49.  
  50. public Object getItem(int position)
  51. {
  52. return position;
  53. }
  54.  
  55. public long getItemId(int position)
  56. {
  57. return position;
  58. }
  59.  
  60. //returns the individual images to the widget as it requires them
  61. public View getView(int position,ViewGroup parent)
  62. {
  63. final ImageView imgView = new ImageView(mContext);
  64.  
  65. imgView.setImageBitmap(mImageArray[position]);
  66.  
  67. //put black borders around the image
  68. final RelativeLayout borderImg = new RelativeLayout(mContext);
  69. borderImg.setPadding(20,20,20);
  70. borderImg.setBackgroundColor(0xff000000);//black
  71. borderImg.addView(imgView);
  72. return borderImg;
  73. }
  74.  
  75. }//end of: class GalleryAdapter
  76.  
  77.  
  78. /**
  79. * Helper Functions
  80. * @throws IOException
  81. */
  82. private Bitmap getBitmapFromAsset(String strName) throws IOException
  83. {
  84. AssetManager assetManager = getAssets();
  85.  
  86. InputStream istr = assetManager.open(strName);
  87. Bitmap bitmap = BitmapFactory.decodeStream(istr);
  88. istr.close();
  89.  
  90. return bitmap;
  91. }
  92. }

猜你在找的Android相关文章