Display all images on android device -
i'm trying create application on 1 of activities displays images on device user chose 1 of them.
i managed display images on dcim/camera folder.
this code displaying images:
gridview gridview = (gridview) findviewbyid(r.id.gridview); imageadapter = new imageadapter(this); gridview.setadapter(imageadapter); gridview.setbackgroundcolor(getresources().getcolor(r.color.black)); string externalstoragedirectorypath = environment.getexternalstoragedirectory().getabsolutepath(); string targetpath = externalstoragedirectorypath + "/dcim/camera"; toast.maketext(getapplicationcontext(), targetpath, toast.length_long).show(); file targetdirector = new file(targetpath); file[] files = targetdirector.listfiles(); (file file : files) imageadapter.add(file.getabsolutepath()); can me here?
thank in advance!
on click (maybe upload button) :
intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select picture"), pick_image); when user has selected image :
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == pick_image && resultcode == activity.result_ok && null != data) { uri selectedimage = data.getdata(); bitmapfactory.options options = new bitmapfactory.options(); //options.insamplesize = 8; final inputstream ist; try { ist = mactivity.getcontentresolver().openinputstream(selectedimage); bitmap bitmap = bitmapfactory.decodestream(ist, null, options); mpicturepath = compressimage(bitmap).getabsolutepath(); //recycling bitmap overcome outofmemoryerror if(bitmap!=null){ bitmap.recycle(); bitmap=null; } ist.close(); //you have mpicturepath it! } catch (filenotfoundexception e) { logouploadfaied(); e.printstacktrace(); } catch (ioexception e) { logouploadfaied(); e.printstacktrace(); } } else { //not uploading } } here compress image code, takes in bitmap argument, stores locally (compressed) , returns file path. excellent if want upload images server whatsapp:
public file compressimage(bitmap bmp) { bitmap scaledbitmap = null; int actualheight = bmp.getheight(); int actualwidth = bmp.getwidth(); // max height , width values of compressed image taken 816x612 float imgratio = actualwidth / actualheight; float maxratio = logomaxwidth / logomaxheight; // width , height values set maintaining aspect ratio of image if (actualheight > logomaxheight || actualwidth > logomaxwidth) { if (imgratio < maxratio) { imgratio = logomaxheight / actualheight; actualwidth = (int) (imgratio * actualwidth); actualheight = (int) logomaxheight; } else if (imgratio > maxratio) { imgratio = logomaxwidth / actualwidth; actualheight = (int) (imgratio * actualheight); actualwidth = (int) logomaxwidth; } else { actualheight = (int) logomaxheight; actualwidth = (int) logomaxwidth; } } try { scaledbitmap = bitmap.createscaledbitmap(bmp, actualwidth, actualheight, true); } catch (outofmemoryerror exception) { logouploadfaied(); exception.printstacktrace(); } string uristing = (system.currenttimemillis() + ".jpg"); file file = new file(environment.getexternalstoragedirectory() + file.separator + uristing); try { file.createnewfile(); } catch (ioexception e) { logouploadfaied(); e.printstacktrace(); } fileoutputstream fos = null; try { fos = new fileoutputstream(file); } catch (filenotfoundexception e) { logouploadfaied(); e.printstacktrace(); } try { fos.write(getbytesfrombitmap(scaledbitmap)); fos.close(); //recycling bitmap overcome outofmemoryerror if(scaledbitmap!=null){ scaledbitmap.recycle(); scaledbitmap=null; } } catch (ioexception e) { logouploadfaied(); e.printstacktrace(); } return file; }
Comments
Post a Comment