gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:55 25 4
gpt4 key购买 nike

我一直致力于创建图像的 GridView ,图像存在于 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 文件夹中读取了图像,但它没有遍历 GridView 中的单元格。 GridView 的所有单元格都具有从图像集中选取的相同图像。谁能告诉我如何遍历单元格并仍然有不同的图像?

我在扩展 BaseAdapter 类的 ImageAdapter 类中有上述代码,在我的主类中,我通过以下方式将其与我的 gridview 链接:

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

非常感谢您提前提供的帮助,萨兰

最佳答案

Saran,下面是我用来显示 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;
}
}

关于Android - GridView 中 Assets 文件夹中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2752924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com