java - no images being displayed in GridView -
i new app development , have been trying create image gallery app. when click open app, nothing being displayed.i expect display images stored on sd card in gridview. have read quite few questions on related haven't been of help. can't find wrong in code.can tell me wrong code?
imagegallery.java :
import android.app.activity; import android.os.bundle; import android.os.environment; import android.widget.gridview; import java.io.file; import java.util.arraylist; public class imagegallery extends activity { arraylist<string> images; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_image_gallery); images = new arraylist<>();// list of file paths getfromsdcard(); gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(new imageadapter(this, images)); } file[] listfile; public void getfromsdcard() { file file = new file(environment.getexternalstoragepublicdirectory( environment.directory_pictures), "inscam + fixblur"); if (listfile != null && listfile.length > 0) { (file file1 : listfile) { if (file1.isdirectory()) { listfile = file.listfiles(); } else { if (file.getname().endswith(".png") || file.getname().endswith(".jpg") || file.getname().endswith(".jpeg") || file.getname().endswith(".gif") || file.getname().endswith(".bmp") || file.getname().endswith(".webp")) { string temp = file1.getpath().substring(0, file1.getpath().lastindexof('/')); if (!images.contains(temp)) images.add(temp); } } } } } }
imageadapter.java :
import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; import java.util.list; public class imageadapter extends baseadapter { private static final int padding = 8; private static final int width = 250; private static final int height = 250; private context mcontext; private list<string> mthumbids; public imageadapter(context c, list<string> ids){ mcontext = c; this.mthumbids = ids; } @override public int getcount() { return mthumbids.size(); } @override public object getitem(int position) { return null; } // called provide id // passed onitemclicklistener.onitemclick() @override public long getitemid(int position) { return position; } // create new imageview each item referenced adapter @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview = (imageview) convertview; // if convertview's not recycled, initialize attributes if (imageview == null) { imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(width, height)); imageview.setpadding(padding, padding, padding, padding); imageview.setscaletype(imageview.scaletype.center_crop); } // first decode injustdecodebounds=true check dimensions final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(mthumbids.get(position), options); // set insamplesize options.insamplesize = 4; // decode bitmap insamplesize set options.injustdecodebounds = false; bitmap mybitmap = bitmapfactory.decodefile(mthumbids.get(position), options); imageview.setimagebitmap(mybitmap); return imageview; } }
your getitem method in adapter returns null seems issue. should return string path or bitmap after converting string path ever return type object. suggest this.
class imageadapter extends baseadapter{ @override public object getitem(int position) { //i have shown string example here.. can update per needs.. return mthumbids.get(position); } }
hope helps..
Comments
Post a Comment