gridview - how to display images saved in sdcard folder in android -
as working on displaying images saved in sdcard folder, found following link.
displaying images in gridview sdcard.
i using following code images specified folder in sdcard,but here getting 0 count.
mycode.jav
string[] projection = {mediastore.images.media._id}; final string[] columns = { mediastore.images.media.data,mediastore.images.media._id }; final string orderby = mediastore.images.media._id; cursor imagecursor = managedquery( mediastore.images.media.external_content_uri, projection, mediastore.images.media.data + " ? ", new string[] {"/my_images"}, null); int image_column_index = imagecursor.getcolumnindex(mediastore.images.media._id); this.count = imagecursor.getcount(); this.thumbnails = new bitmap[this.count]; this.arrpath = new string[this.count]; this.thumbnailsselection = new boolean[this.count]; (int = 0; < this.count; i++) { imagecursor.movetoposition(i); int id = imagecursor.getint(image_column_index); int datacolumnindex = imagecursor .getcolumnindex(mediastore.images.media.data); thumbnails[i] = mediastore.images.thumbnails.getthumbnail( getapplicationcontext().getcontentresolver(), id, mediastore.images.thumbnails.micro_kind, null); arrpath[i] = imagecursor.getstring(datacolumnindex); } imageadapter = new imageadapter(); secure_gallery_grid.setadapter(imageadapter); imagecursor.close();
but here per following link images saved in sd card displaying. but here want display images saved in particular folder, created "my_images" folder & saved images in folder. want display images folder & need display in gridview per link.
you can path of files particualr folder below
once path of files ca display images in gridview
arraylist<string> f = new arraylist<string>();// list of file paths file[] listfile; public void getfromsdcard() { file file= new file(android.os.environment.getexternalstoragedirectory(),"tmyfolder"); if (file.isdirectory()) { listfile = file.listfiles(); (int = 0; < listfile.length; i++) { f.add(listfile[i].getabsolutepath()); } } }
remember add permissionin manifest file
<uses-permission android:name="android.permission.write_external_storage"/>
by having write permission have read permission default.
example
main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <gridview android:id="@+id/phoneimagegrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnwidth="90dp" android:gravity="center" android:horizontalspacing="10dp" android:numcolumns="auto_fit" android:stretchmode="columnwidth" android:verticalspacing="10dp" /> </relativelayout>
gelleryitem.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <imageview android:id="@+id/thumbimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" /> <checkbox android:id="@+id/itemcheckbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" /> </relativelayout>
androidcustomgalleryactivity.java
public class androidcustomgalleryactivity extends activity { private int count; private bitmap[] thumbnails; private boolean[] thumbnailsselection; private string[] arrpath; private imageadapter imageadapter; arraylist<string> f = new arraylist<string>();// list of file paths file[] listfile; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); getfromsdcard(); gridview imagegrid = (gridview) findviewbyid(r.id.phoneimagegrid); imageadapter = new imageadapter(); imagegrid.setadapter(imageadapter); } public void getfromsdcard() { file file= new file(android.os.environment.getexternalstoragedirectory(),"maplebear"); if (file.isdirectory()) { listfile = file.listfiles(); (int = 0; < listfile.length; i++) { f.add(listfile[i].getabsolutepath()); } } } public class imageadapter extends baseadapter { private layoutinflater minflater; public imageadapter() { minflater = (layoutinflater) getsystemservice(context.layout_inflater_service); } public int getcount() { return f.size(); } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = minflater.inflate( r.layout.galleryitem, null); holder.imageview = (imageview) convertview.findviewbyid(r.id.thumbimage); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } bitmap mybitmap = bitmapfactory.decodefile(f.get(position)); holder.imageview.setimagebitmap(mybitmap); return convertview; } } class viewholder { imageview imageview; } }
snap shot
Comments
Post a Comment