Android GridView显示SD卡的图片

GridView的XML布局:

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:

 public class FileimageActivity extends Activity {
       private static final String TAG = "Fileimage";
       private List<HashMap<String, String>> list;
       private ContentResolver cr;
       private List<ImageView> imageViews = null;
       HashMap<String,Object> hashMap;
       public Bitmap bitmap;
       public Bitmap 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{
         public picview(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


               */
              public void 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));
                     }
              }            
       }
      
}
原文地址:https://www.cnblogs.com/zhujiabin/p/4208490.html