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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2752924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:13:24  来源:igfitidea点击:

Android - Images from Assets folder in a GridView

androidimageandroid-gridview

提问by Saran

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 androidlink helped me with using the bitmap to read it. The code am currently having is:

我一直致力于创建图像的网格视图,图像存在于 Assets 文件夹中。从 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();
   }
  }

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 ?

我的应用程序确实从 Assets 文件夹中读取图像,但它没有遍历网格视图中的单元格。网格视图的所有单元格都具有从图像集中挑选的相同图像。谁能告诉我如何遍历单元格并仍然有不同的图像?

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:

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

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

Thanks a lot for any help in advance, Saran

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

回答by Someone Somewhere

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:

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, 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;
    }
}

回答by fak1r

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.

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

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