Android GridView display image of SD card

Source: Internet
Author: User

XML Layout of the GridView :

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 Listlist;       Private Contentresolver CR; Private List<ImageView> imageviews =NULL; HashMap<String,Object>HashMap;       Public Bitmap Bitmap;     Public Bitmap newbit;  Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);     Findimagepath (); }      Public voidFindimagepath () Gridviewgridview=(GridView) Findviewbyid (R.id.gridview); List=newarraylist(); CR=Getcontentresolver (); String[]imagedata={thumbnails._id,thumbnails.image_id,thumbnails.data};//Find ImageCursorcursor=cr.query (Thumbnails.external_content_uri, ImageData,NULL,                     NULL,NULL);//using cursors to find an imageString[] from= {"image_id","Path" }; int[] to = {r.id.textid, r.id.itemimage};//in the layout of the Imageview.xmlgetcolumndata (cursor); Listadapteradapter=NewPicview ( This, List, R.layout.imageitem, from, to);    Gridview.setadapter (adapter); }//find the path to the image and put it in the list.   Private voidgetcolumndata (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 =NewHashmap<string, string>(); Hash.put ("image_id", image_id +""); Hash.put ("Path", Image_path);                     List.add (hash); }                       while(Cur.movetonext ()); }                    }          //many times the GridView is using Baseadapter to display pictures more, baseadapter of the picture resources from drawable resources. But its practical simpleadapter is already available,//the point is that there is setviewimage in Simpleadapter.         classPicview 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 a ImageView but only if there is no existing viewbinder or               I * f the existing Viewbinder Cannothandle binding to an ImageView.                * By default, the value would 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 a int orint               Eger.  * @ Value The address of image is 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=NewFileInputStream (value); Bufferedinputstreambis=NewBufferedinputstream (FIS); Bitmap=Bitmapfactory.decodestream (bis); Newbit= Bitmap.createscaledbitmap (Bitmap, the, the,false);                            V.setimagebitmap (Newbit);                            Bis.close ();                     Fis.close (); }                       Catch(Exceptione) {LOG.E (tag,e.tostring ());                     V.setimageuri (Uri.parse (value)); }              }                   }      }

Android GridView display image of SD card

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.