Android中Gallery显示手机中的图片

摘要:
在网上找了很长时间后,似乎没有关于这方面的信息,所以请自己填补空白。常见的方法是定义一个数组来存储图片的引用,例如:1PrivateIntegrate[]pictures={2R.drawable.icon1,3R.drawable.icon2,4R.drawable.icon3,5R.drawable/icon4,6R.drawable.icon57};然后将此数组添加到适配器中。但如果你想展示你的照片,你怎么能做到呢。。让我介绍一下我自己的方法。首先,您需要知道图片的存储路径。将这些图片的路径存储在数组列表中,1ArrayList<String>PictureNameList=newArrayList<String˃();2PicureNameList.add;3…4…然后获取画廊实例1Galleryg=findViewById;相应的mygallery.xml01˂?

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

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

 

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 BaseAdapter
02    {
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" />

 



源码下载


免责声明:文章转载自《Android中Gallery显示手机中的图片》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android类参考---SQLiteOpenHelperGrid表格的js触发事件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

安卓渗透和审计工具整理

1.cSploit: https://github.com/cSploit/android/releases 2.DroidSheephttp://bbs.zhiyoo.com/thread-13249611-1-1.html 3.androrathttps://github.com/wszf/androrat 4.Network Spoofhttps:/...

安卓系统手机目录

一、文件夹 1./acct/    系统回收站,删除的系统文件。     2./cache/    缓存     3./data/    用户的所有程序相关数据     app/    所有用户安装的apk文件     app-private/     data/    每一个应用的数据         com.xx.appname/    每一个应用的数...

js定位光标到输入框指定位置

在提供友好用户界面时,常常要定位光标到输入框的指定位置。通常是尾部,好让用户接着输入信息。我们可以用javascript操作dom来实现,以下是实现的方法   1.       function changeCursor(input, position) {    2.             var range = input.createTextRa...

APP专项测试

一、功能测试 功能测试主要根据软件需求说明书或用户需求等资料,编写测试用例,以验证各个功能点的实现情况,具体实施过程参考如下: 根据被测试模块的功能点,设计相应的测试用例进行覆盖,例如涉及到用户输入的地方要考虑到边界,用户使用场景类要考虑到正常和异常的场景,业务相关联的模块需要联合测试等; 随时关注跟踪需求的变化和理解需求,对需求理解有误时及时更改相关测...

3D画廊

3D画廊 之前我都是写的学习的内容,我在写这些教程时遇到有趣的炫酷的小例子也会专门拿出来写一篇文章,今天就写一个酷炫的小例子,叫3D画廊,它是属于ViewPage的进阶版。 此项目下载地点:https://github.com/qySvip/3D-gallery 下面的指示器是使用的一大神的第三方库,会在文章下方简单讲述一下。 效果图 3D画廊的实现 首...

Android 自定义CheckBox 样式

新建Android XML文件,类型选Drawable,根结点选selector,在这定义具体的样式。 <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">...