android - Displaying images from a specific folder on the SDCard using a gridview -
i'm attempting create gridview loaded images specific folder resides on sdcard. path folder known, ("/sdcard/pictures") , in examples i've seen online unsure how or specify path pictures folder want load images from. have read through dozens of tutorials, hellogridview tutorial @ developer.android.com tutorials not teach me seeking.
every tutorial have read far has either:
a) called images drawable /res folder , put them array loaded, not using sdcard @ all.
b) accessed all pictures on sdcard using mediastore not specifying how set path folder want display images form
or
c) suggested using bitmapfactory, haven't slightest clue how use.
if i'm going in wrong way, please let me know , direct me toward proper method i'm trying do.
ok, after many iterations of trying, have example works , thought i'd share it. example queries images mediastore, obtains thumbnail each image display in view. loading images gallery object, not requirement code work:
make sure have cursor , int column index defined @ class level gallery's imageadapter has access them:
private cursor cursor; private int columnindex;
first, obtain cursor of image ids located in folder:
gallery g = (gallery) findviewbyid(r.id.gallery); // request image id returned string[] projection = {mediastore.images.media._id}; // create cursor pointing sdcard cursor = managedquery( mediastore.images.media.external_content_uri, projection, mediastore.images.media.data + " ? ", new string[] {"%myimagesfolder%"}, null); // column index of image id columnindex = cursor.getcolumnindexorthrow(mediastore.images.media._id); g.setadapter(new imageadapter(this));
then, in imageadapter gallery, obtain thumbnail display:
public view getview(int position, view convertview, viewgroup parent) { imageview = new imageview(context); // move cursor current position cursor.movetoposition(position); // current value requested column int imageid = cursor.getint(columnindex); // obtain image uri uri uri = uri.withappendedpath( mediastore.images.media.external_content_uri, integer.tostring(imageid) ); string url = uri.tostring(); // set content of image based on image uri int originalimageid = integer.parseint(url.substring(url.lastindexof("/") + 1, url.length())); bitmap b = mediastore.images.thumbnails.getthumbnail(getcontentresolver(), originalimageid, mediastore.images.thumbnails.mini_kind, null); i.setimagebitmap(b); i.setlayoutparams(new gallery.layoutparams(150, 100)); i.setscaletype(imageview.scaletype.fit_xy); i.setbackgroundresource(mgalleryitembackground); return i; }
i guess important section of code managedquery demonstrates how use mediastore queries filter list of image files in specific folder.
Comments
Post a Comment