Out of Memory on Bitmap Android -


i want make implement in android enter image description here this, have load images asset folder , making 2 horizontalscrollview in xml file , load dynamically imageview in it. loading imageview using code

linearlayout mygallery = (linearlayout) findviewbyid(r.id.mygallery);  try {     string gallerydirectoryname = "gallery";     string[] listimages = getassets().list(gallerydirectoryname);     (string imagename : listimages) {         inputstream = getassets().open(gallerydirectoryname + "/" + imagename);         bitmap bitmap = bitmapfactory.decodestream(is);          imageview imageview = new imageview(getapplicationcontext());         imageview.setlayoutparams(new viewgroup.layoutparams(100, 100));          //   imageview.setscaletype(imageview.scaletype.center_crop);         imageview.setimagebitmap(bitmap);          linearlayout.layoutparams mygallery1=  new linearlayout.layoutparams(100, 100);         mygallery1.setmargins(20, 0,  10, 0);          //its working         // imageview.setlayoutparams(mygallery1);                 imageview.setonclicklistener(new view.onclicklistener() {             public void onclick(view view) {                 //   diplayimage.setimagebitmap(bitmap);             }         });          log.e("gallerywithhorizontalscrollview", e.getmessage(), e);     } 

//repeating above code load imageview in second horizontalscrollview

linearlayout mygallery2 = (linearlayout) findviewbyid(r.id.mygallery2);  try {     string gallerydirectoryname1 = "gallery2";     string[] listimages2 = getassets().list(gallerydirectoryname1);     (string imagename : listimages2) {         inputstream is1 = getassets().open(gallerydirectoryname1 + "/" + imagename);         bitmap bitmap1 = bitmapfactory.decodestream(is1);          imageview imageview = new imageview(getapplicationcontext());         imageview.setlayoutparams(new viewgroup.layoutparams(100, 100));          //  imageview.setscaletype(imageview.scaletype.center_crop);         imageview.setimagebitmap(bitmap1);          linearlayout.layoutparams mygallery21=  new linearlayout.layoutparams(100, 100);         mygallery21.setmargins(20, 40,  10, 0);          //its working          mygallery.addview(imageview,mygallery1);     } } catch (ioexception e) {     mygallery2.addview(imageview,mygallery21);         } } catch (ioexception e) {     log.e("gallerywithhorizontalscrollview", e.getmessage(), e); } } 

if make 1 horizontalscrollview , load imageviews in works fine second horizontalscrollview gives me error @ line 76

bitmap bitmap1 = bitmapfactory.decodestream(is1); 

and log cat this

05-14 11:13:26.000: e/androidruntime(8350): fatal exception: main 05-14 11:13:26.000: e/androidruntime(8350): java.lang.outofmemoryerror 05-14 11:13:26.000: e/androidruntime(8350):     @ android.graphics.bitmapfactory.nativedecodeasset(native method) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:577) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:643) 05-14 11:13:26.000: e/androidruntime(8350):     @ com.example.gallery.mainactivity.oncreate(mainactivity.java:76) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.app.activity.performcreate(activity.java:4470) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1053) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1934) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:1995) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.app.activitythread.access$600(activitythread.java:128) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.app.activitythread$h.handlemessage(activitythread.java:1161) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.os.handler.dispatchmessage(handler.java:99) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.os.looper.loop(looper.java:137) 05-14 11:13:26.000: e/androidruntime(8350):     @ android.app.activitythread.main(activitythread.java:4517) 05-14 11:13:26.000: e/androidruntime(8350):     @ java.lang.reflect.method.invokenative(native method) 05-14 11:13:26.000: e/androidruntime(8350):     @ java.lang.reflect.method.invoke(method.java:511) 05-14 11:13:26.000: e/androidruntime(8350):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) 05-14 11:13:26.000: e/androidruntime(8350):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:760) 05-14 11:13:26.000: e/androidruntime(8350):     @ dalvik.system.nativestart.main(native method) 

you can find answer here,

and important thing should consider optimize bitmap thumbnail. try :

public static bitmap decodesampledbitmapfrominput(context context,         inputstream input, int reqwidth, int reqheight) {      // first decode injustdecodebounds=true check dimensions     final bitmapfactory.options options = new bitmapfactory.options();     options.injustdecodebounds = true;     bitmapfactory.decodestream(input, null, options);      // calculate insamplesize     options.insamplesize = calculateinsamplesize(options, reqwidth,             reqheight);      // decode bitmap insamplesize set     options.injustdecodebounds = false;     return bitmapfactory.decodestream(input, null, options); } 

here reqwidth width of thumbnail , same reqheight.

and listview try implement cache bitmaps , try them using unique key may file name (because should not save same images multiple times)


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -