Android images gridview -
the code display images sd card , whatsapp in app when run code pictures displayed there random whitespaces between photos , photos not of same size. how resize image ? heres code main activity:
import java.io.file; import java.util.arraylist; import android.app.actionbar; import android.app.activity; import android.content.context; import android.graphics.bitmapfactory; import android.os.bundle; import android.os.environment; import android.provider.mediastore; import android.view.window; import android.widget.baseadapter; import android.widget.gridview; import android.widget.toast; public class mainactivity extends activity { private imageadapter imageadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview gridview = (gridview) findviewbyid(r.id.gridview); imageadapter = new imageadapter(this); gridview.setadapter(imageadapter); string externalstoragedirectorypath = environment .getexternalstoragedirectory() .getabsolutepath(); string targetpath = externalstoragedirectorypath + "/dcim/camera"; file f=new file(environment.getexternalstoragedirectory()+"/whatsapp"); if(f.exists()){ string path=environment.getexternalstoragedirectory()+"/whatsapp/media/whatsapp images"; file t=new file(path); file[] fo=t.listfiles(); for(file fil : fo){ imageadapter.add(fil.getabsolutepath()); } } toast.maketext(getapplicationcontext(), targetpath, toast.length_long).show(); file targetdirector = new file(targetpath); file[] files = targetdirector.listfiles(); (file file : files) { imageadapter.add(file.getabsolutepath()); } }
heres code image adapter:
import java.util.arraylist; 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; public class imageadapter extends baseadapter { private context context; arraylist<string> imagelist = new arraylist<string>(); public imageadapter(context c) { context = c; } void add(string path){ imagelist.add(path); } @override public int getcount() { return imagelist.size(); } @override public object getitem(int arg0) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if (convertview == null) { imageview = new imageview(context); imageview.setlayoutparams(new gridview.layoutparams(100,100)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(8, 8, 8, 8); } else { imageview = (imageview) convertview; } bitmap bm = decodesampledbitmapfromuri(imagelist.get(position), 100,100); imageview.setimagebitmap(bm); return imageview; } public bitmap decodesampledbitmapfromuri(string path, int reqwidth, int reqheight) { bitmap bm = null; final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(path, options); options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); options.injustdecodebounds = false; bm = bitmapfactory.decodefile(path, options); return bm;
}
public int calculateinsamplesize( bitmapfactory.options options, int reqwidth, int reqheight) { final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { if (width > height) { insamplesize = math.round((float)height / (float)reqheight); } else { insamplesize = math.round((float)width / (float)reqwidth); } } return insamplesize;
}
as said before, code runs sizee of image not uniform , there whitespaces. please me solve it.
try set scale type fit_xy
imageview.setscaletype(imageview.scaletype.fit_xy);
Comments
Post a Comment