【Tech-Android-View】Android中利用GridView显示SD卡的图片

 其实学习Andriod已经有一段时间了,因为之前一直不是工作在这些语言上,纯粹出于个人爱好,不过最近想投身于此行业了,对于没有任何工作经验的我,找到个工作是相当的难。

       1.无经验 2.语言功底不够

       经验我是无法改变的,语言功底估计要积累一下,所以我开了个blog来写写一些学习心得,和人分享的同时加强自己对语言的记忆运用。

        最近看到一个连连看的游戏源码,然后就研究了下图片的显示。想做个简单的显示手机内图片的程序,就有了以下的程序。

    第一个是GridViewXML布局:<main.xml>    

        

<GridViewxmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/gridview"  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"  
    android:numColumns="auto_fit"  
    android:verticalSpacing="10dp"  
    android:horizontalSpacing="10dp"  
    android:columnWidth="90dp"  
    android:stretchMode="columnWidth"  
    android:gravity="center"  
/> 



<imageitem.xml>的布局代码:




<RelativeLayout   
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_height="wrap_content"   
         android:paddingBottom="4dip"android:layout_width="fill_parent">  
         <TextView 
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:id="@+id/textid"/>
         <ImageView   
              android:layout_height="wrap_content"   
              android:id="@+id/ItemImage"   
               android:layout_width="wrap_content"  
              android:layout_centerHorizontal="true">   
         </ImageView>  


</RelativeLayout>  


 

 

下面是ACtivity的代码,只要一个Activity就可以,包得导入省去:

public class FileimageActivity extends Activity {
    
       
       privatestatic final String TAG = "Fileimage";
       privateList<HashMap<String, String>> list;
       privateContentResolver cr;
       privateList<ImageView> imageViews = null;
       HashMap<String,Object> hashMap;
       publicBitmap bitmap;
       publicBitmap newBit;
       


    public void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       findimagepath();
        
    }
       public void findimagepath()
{
      GridViewgridView=(GridView)findViewById(R.id.gridview);
      list=newArrayList<HashMap<String,String>>();
      cr=getContentResolver();
      String[]imagedata={Thumbnails._ID,Thumbnails.IMAGE_ID,Thumbnails.DATA};//找到image
      Cursorcursor=cr.query(Thumbnails.EXTERNAL_CONTENT_URI, imagedata, null, 
                    null,null);                                                                                                         //利用游标找image
      String[]from = { "image_id", "path" };
              int[]to = { R.id.textid, R.id.ItemImage };                                                               //在imageView.xml的布局中
      getColumnData(cursor);
      
      ListAdapteradapter = new picview(this, list, R.layout.imageitem,
                            from,to);
      gridView.setAdapter(adapter);
    }
//找到image的path,再放进list里。
  private void getColumnData(Cursor cur)
 {
              if(cur.moveToFirst())
              {
                     int_id;
                     intimage_id;
                     Stringimage_path;
                     int_idColumn = cur.getColumnIndex(Thumbnails._ID);
                     intimage_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);
                     intdataColumn = cur.getColumnIndex(Thumbnails.DATA);
                     
            Log.i(TAG, String.valueOf(_idColumn)) ;
            
                     do
                     {
                            //Get the field values
                            _id= cur.getInt(_idColumn);
                            image_id= cur.getInt(image_idColumn);
                            image_path= cur.getString(dataColumn);


                            //Do something with the values.
                            //Log.i(TAG, _id + " image_id:" + image_id + " path:"
                            //+ image_path + "---");
                            HashMap<String,String> hash = new HashMap<String, String>();
                            hash.put("image_id",image_id + "");
                            hash.put("path",image_path);
                            list.add(hash);


                     } 
                     while(cur.moveToNext());


              }
              
       }
          //很多时候GridView是利用BaseAdapter来显示图片更多,BaseAdapter的图片资源多少来自drawable的资源。不过其实用SimpleAdapter就已经可以,
        //重点是SimpleAdapter中有setViewImage这个方法。
        class picview extends SimpleAdapter{


              publicpicview(Context context, List<? extends Map<String, ?>> data,
                            intresource, String[] from, int[] to) {
                     super(context,data, resource, from, to);
              
              }
              /*
               * Called by bindView() to set theimage for an ImageView but only if there is no existing ViewBinder or i
               * f the existing ViewBinder cannothandle binding to an ImageView.
               *  By default, the value will betreated as an image resource. 
               *  If the value cannot be used asan image resource, the value is used as an image Uri.
               *   This method is called insteadof setViewImage(ImageView, int) 
               * if the supplied data is not an int orInteger.
               * @ value为image的地址即为path
               * Parameters
               * v  ImageView to receive an image
           value  the value retrieved from the data set


               */
              publicvoid setViewImage(ImageView v, String value)
              {
                     try
                     {
                            FileInputStreamfis = new FileInputStream(value);
                            BufferedInputStreambis = new BufferedInputStream(fis);
                            bitmap= BitmapFactory.decodeStream(bis);
                            newBit= Bitmap.createScaledBitmap(bitmap, 180, 180, false);
                            v.setImageBitmap(newBit);


                            bis.close();
                            fis.close();
                     }
                       catch (Exceptione)
                     {
                            Log.e(TAG,e.toString());
                            v.setImageURI(Uri.parse(value));
                     }
              }             
       }
       
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值