Android中Gallery展示手机中的图片

Android中Gallery显示手机中的图片在网上找了好久乎都没有关于这方面的(可能是自己的信息量太小吧),于是自

Android中Gallery显示手机中的图片

在网上找了好久似乎都没有关于这方面的(可能是自己的信息量太小吧),于是自己来填补这个漏洞吧。

常见的方法莫过于自己定义一个数组,用以存储图片的引用,如:

1private Integer[] pictures = { 2        R.drawable.icon1, 3        R.drawable.icon2, 4        R.drawable.icon3, 5        R.drawable.icon4, 6        R.drawable.icon5 7};然后再将此数组加入到适配器中。但是想要显示自己的图片,这怎么行。。下面来介绍我自己的方法


首先,你得知道你图片的存储路径,将这些你将要显示的图片的路径存放于一个arraylist里面

1ArrayList<String> PictureNameList = new ArrayList<String>();2         PicureNameList.add("路径");3                 ...4                 ...然后获取gallery实例,

1Gallery g = (Gallery) findViewById(R.id.mygallery);相应的mygallery.xml

01<?xml version="1.0" encoding="utf-8"?>02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"03    android:layout_width="fill_parent"04    android:layout_height="fill_parent"05    android:orientation="vertical"06>07 08  <!-- 建立一個  Gallery -->09  <Gallery 10  android:id="@+id/mygallery"11  android:layout_width="fill_parent"12  android:layout_height="wrap_content"13  android:layout_x="12px"14  android:layout_y="106px" 15  />16   17   18</LinearLayout>

并加入到适配器里面

01g.setAdapter(new ImageAdapter(this,PictureNameList));02 03        /*设定一个itemclickListener事件*/04        g.setOnItemClickListener(new OnItemClickListener()05        {06          public void onItemClick(AdapterView<?> parent,07                           View v, int position, long id)08          {09 10           //这里就根据你自己的需要去做一些功能的展示11          }12        });下面就来看这个ImageAdaper实现

01public class ImageAdapter extends BaseAdapter02    {03      /*声明变量*/04      int mGalleryItemBackground;05      private Context mContext;06      private List<String> lis;07       08      /*ImageAdapter的构造符*/09      public ImageAdapter(Context c,List<String> li)10      {11        mContext = c;12        lis=li;13        /* 使用res/values/attrs.xml中的<declare-styleable>定义14        * 的Gallery属性.*/15        TypedArray a = obtainStyledAttributes(R.styleable.Gallery);16        /*取得Gallery属性的Index id*/17        mGalleryItemBackground = a.getResourceId(18            R.styleable.Gallery_android_galleryItemBackground, 0);19        /*让对象的styleable属性能够反复使用*/20        a.recycle();21      }22       23      /*几定要重写的方法getCount,传回图片数目*/24      public int getCount()25      {26        return lis.size();27      }28       29      /*一定要重写的方法getItem,传回position*/30      public Object getItem(int position)31      {32        return position;33      }34       35      /*一定要重写的方法getItemId,传并position*/36      public long getItemId(int position)37      {38        return position;39      }40       41      /*几定要重写的方法getView,传并几View对象*/42      public View getView(int position, View convertView,43                            ViewGroup parent)44      {45        /*产生ImageView对象*/46        ImageView i = new ImageView(mContext);47        /*设定图片给imageView对象*/48        Bitmap bm = BitmapFactory.decodeFile(lis.49                              get(position).toString());50        i.setImageBitmap(bm);51        /*重新设定图片的宽高*/52        i.setScaleType(ImageView.ScaleType.FIT_XY);53        /*重新设定Layout的宽高*/54        i.setLayoutParams(new Gallery.LayoutParams(200, 120));55        /*设定Gallery背景图*/56        i.setBackgroundResource(mGalleryItemBackground);57        /*传回imageView对象*/58        return i;59      }    60    }61  }

其中只需要关注最后一个getView函数,这个函数的关键就在于其中两行

1Bitmap bm = BitmapFactory.decodeFile(lis.2                              get(position).toString());3        i.setImageBitmap(bm);获取图片并显示图片。ok!

记得在values文件下里面添加一个叫做attrs.xml的文件

1<?xml version="1.0" encoding="utf-8"?>2<resources>3  <declare-styleable name="Gallery">4    <attr name="android:galleryItemBackground" />5  </declare-styleable>6</resources>最后还要记得在配置文件中添加权限

1<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />2    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />



源码下载