Android - GridView 中资产文件夹中的图像
我一直致力于创建图像的网格视图,图像存在于资产文件夹中。 从 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));
非常感谢您提前提供的任何帮助, 萨兰
I have been working on creating a Grid View of images, with images being present in the Assets folder. Opening a File from assets folder in android link helped me with using the bitmap to read it. The code am currently having is:
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();
}
}
My application does read the image from the Assets folder, but it is not iterating through the cells in the grid view. All the cells of the grid view have a same image picked from the set of images. Can anyone tell me how to iterate through the cells and still have different images ?
I have the above code in an ImageAdapter Class which extends the BaseAdapter class, and in my main class I am linking that with my gridview by:
GridView gv =(GridView)findViewById(R.id.gridview);
gv.setAdapter(new ImageAdapter(this, assetlist));
Thanks a lot for any help in advance,
Saran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Saran,下面是我用来显示资源文件夹中的图像和图库的内容。我想这与 gridview 的处理是一样的:
Saran, below is what I use to show images in the assets folder with the gallery. I imagine it's the same deal with a gridview:
无需每次都阅读所有项目。只读取 getView 方法调用中给定位置的项目。并且当时只显示该项目。
No need to read all the items every time. Read only the item at the position given in getView method call. And display only that item that time.