performance - Android adding image to app, makes it slow -


i have imagebutton, when click it gives me alertdialog add image imagebutton. when add image, app becomes slow , laggy. if want reselect image app crashes. can me out this?

code:

public class mainactivity extends activity {  private final int select_file = 1; private final int request_camera = 0; private imagebutton btnimg;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      btnimg = (imagebutton) findviewbyid(r.id.btnaddthumbnail);      btnimg.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             selectimage();         }     }); }  private void selectimage() {     final charsequence[] items = { "take photo", "choose gallery",             "cancel" };      alertdialog.builder builder = new alertdialog.builder(mainactivity.this);     builder.settitle("add photo!");      builder.setitems(items, new dialoginterface.onclicklistener() {         @override         public void onclick(dialoginterface dialog, int item) {             if (items[item].equals("take photo")) {                 intent intent = new intent(mediastore.action_image_capture);                 file f = new file(android.os.environment.getexternalstoragedirectory(), "temp.jpg");                 intent.putextra(mediastore.extra_output, uri.fromfile(f));                 startactivityforresult(intent, request_camera);             }              else if (items[item].equals("choose gallery")) {                 intent intent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri);                 intent.settype("image/*");                 startactivityforresult(intent.createchooser(intent, "select file"), select_file);             }              else if (items[item].equals("cancel")) {                 dialog.dismiss();             }         }     });     builder.show(); }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);     if (resultcode == result_ok) {         if (requestcode == request_camera) {             file f = new file(environment.getexternalstoragedirectory().tostring());             (file temp : f.listfiles()) {                 if (temp.getname().equals("temp.jpg")) {                     f = temp;                     break;                 }             }             try {                 bitmap bm;                 bitmapfactory.options btmapoptions = new bitmapfactory.options();                  bm = bitmapfactory.decodefile(f.getabsolutepath(),btmapoptions);                  // bm = bitmap.createscaledbitmap(bm, 70, 70, true);                 btnimg.setimagebitmap(bm);                 btnimg.setscaletype(scaletype.center_inside);                  string path = android.os.environment.getexternalstoragedirectory() + file.separator + "phoenix" + file.separator + "default";                 f.delete();                 outputstream fout = null;                 file file = new file(path, string.valueof(system.currenttimemillis()) + ".jpg");                 try {                     fout = new fileoutputstream(file);                     bm.compress(bitmap.compressformat.jpeg, 85, fout);                     fout.flush();                     fout.close();                 } catch (filenotfoundexception e) {                     e.printstacktrace();                 } catch (ioexception e) {                     e.printstacktrace();                 } catch (exception e) {                     e.printstacktrace();                 }             } catch (exception e) {                 e.printstacktrace();             }         } else if (requestcode == select_file) {             uri selectedimageuri = data.getdata();              string temppath = getpath(selectedimageuri, mainactivity.this);             bitmap bm;             bitmapfactory.options btmapoptions = new bitmapfactory.options();             bm = bitmapfactory.decodefile(temppath, btmapoptions);             btnimg.setimagebitmap(bm);             btnimg.setscaletype(scaletype.center_inside);         }     } }  public string getpath(uri uri, activity activity) {     string[] projection = { mediacolumns.data };     cursor cursor = activity.managedquery(uri, projection, null, null, null);     int column_index = cursor.getcolumnindexorthrow(mediacolumns.data);     cursor.movetofirst();     return cursor.getstring(column_index); } 

}

error after crash:

05-04 20:45:02.890: w/dalvikvm(22576): threadid=1: thread exiting uncaught exception (group=0x41d9f2a0) 05-04 20:45:02.895: e/androidruntime(22576): fatal exception: main 05-04 20:45:02.895: e/androidruntime(22576): java.lang.outofmemoryerror 05-04 20:45:02.895: e/androidruntime(22576): @ android.graphics.bitmapfactory.nativedecodestream(native method) 05-04 20:45:02.895: e/androidruntime(22576): @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:650) 05-04 20:45:02.895: e/androidruntime(22576): @ android.graphics.bitmapfactory.decodefile(bitmapfactory.java:389) 05-04 20:45:02.895: e/androidruntime(22576): @ com.example.spui.mainactivity.onactivityresult(mainactivity.java:95) 05-04 20:45:02.895: e/androidruntime(22576): @ android.app.activity.dispatchactivityresult(activity.java:5390) 05-04 20:45:02.895: e/androidruntime(22576): @ android.app.activitythread.deliverresults(activitythread.java:3178) 05-04 20:45:02.895: e/androidruntime(22576): @ android.app.activitythread.handlesendresult(activitythread.java:3225) 05-04 20:45:02.895: e/androidruntime(22576): @ android.app.activitythread.access$1100(activitythread.java:140) 05-04 20:45:02.895: e/androidruntime(22576): @ android.app.activitythread$h.handlemessage(activitythread.java:1275) 05-04 20:45:02.895: e/androidruntime(22576): @ android.os.handler.dispatchmessage(handler.java:99) 05-04 20:45:02.895: e/androidruntime(22576): @ android.os.looper.loop(looper.java:137) 05-04 20:45:02.895: e/androidruntime(22576): @ android.app.activitythread.main(activitythread.java:4898) 05-04 20:45:02.895: e/androidruntime(22576): @ java.lang.reflect.method.invokenative(native method) 05-04 20:45:02.895: e/androidruntime(22576): @ java.lang.reflect.method.invoke(method.java:511) 05-04 20:45:02.895: e/androidruntime(22576): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 05-04 20:45:02.895: e/androidruntime(22576): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 05-04 20:45:02.895: e/androidruntime(22576): @ dalvik.system.nativestart.main(native method)

the chances image large , running out of memory. devices record images @ quite large image sizes large dimensions, in turn causes run memory issues. same thing happens when dealing images downloaded web.

i scale bitmap down using api method bitmap.createscaledbitmap scale bitmap down before set it.

there post on here android: resize large bitmap file scaled output file

i add great library androidquery, make life simpler! added code below show how works snippet. there full blog post here http://blog.androidquery.com/2011/05/down-sample-images-to-avoid-out-of.html

file file = new file(environment.getexternalstoragedirectory().tostring());        //load image file, down sample target width of 300 pixels , set imageview aq.id(r.id.avatar).image(file, 300); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -