Android的 - 从资产的文件夹在GridView图片 [英] Android - Images from Assets folder in a GridView

查看:145
本文介绍了Android的 - 从资产的文件夹在GridView图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力创造一个网格视图图像,以图像为present在Assets文件夹。 <一href="http://stackoverflow.com/questions/1933015/opening-an-image-file-inside-the-assets-folder">http://stackoverflow.com/questions/1933015/opening-an-image-file-inside-the-assets-folder链接帮我使用位图来阅读。目前遇到的code点是:

I have been working on creating a Grid View of images, with images being present in the Assets folder. http://stackoverflow.com/questions/1933015/opening-an-image-file-inside-the-assets-folder 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 ?

我上面的code在ImageAdapter类扩展了BaseAdapter类,并在我的主类,我链接,与我的GridView方式:

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

推荐答案

萨兰,下面是我用它来显示与画廊资产文件夹的图像。我想这是相同的处理一个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:

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图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆