android - How to reduce image size while taking from gallery through imagepath -
i want take image camera , take gallery part did . when take images 1 activity other through image path show memory out error on taking fourth image ,so want take image gallery , when take image gallery should compressed should not have out of memory error byte allocation.
thanks.
here code can in this
imageview1=(imageview) findviewbyid(r.id.image1); imageview2=(imageview) findviewbyid(r.id.image2); imageview3=(imageview) findviewbyid(r.id.image3); imageview4=(imageview) findviewbyid(r.id.image4); bitmap b = (bitmap) getintent().getparcelableextra("data") ; sharedpreferences preferences = preferencemanager.getdefaultsharedpreferences(this); locationname = preferences.getstring("location", "location"); sharedpreferences preferences1 = preferencemanager.getdefaultsharedpreferences(this); categoryname = preferences1.getstring("categoryname", "categoryname"); sharedpreferences imagepath1 = preferencemanager.getdefaultsharedpreferences(this); selectedimagepath1 = imagepath1.getstring("picturepath1", "picturepath1"); sharedpreferences imagepath2 = preferencemanager.getdefaultsharedpreferences(this); selectedimagepath2 = imagepath2.getstring("picturepath2", "picturepath2"); sharedpreferences imagepath3 = preferencemanager.getdefaultsharedpreferences(this); selectedimagepath3 = imagepath3.getstring("picturepath3", "picturepath3"); sharedpreferences imagepath4 = preferencemanager.getdefaultsharedpreferences(this); selectedimagepath4 = imagepath4.getstring("picturepath4", "picturepath4"); bitmap thumbnail1 = (bitmapfactory.decodefile(selectedimagepath1)); bytearrayoutputstream stream = new bytearrayoutputstream(); thumbnail1.compress(bitmap.compressformat.jpeg, 90, stream); bitmap thumbnail2 = (bitmapfactory.decodefile(selectedimagepath2)); bytearrayoutputstream stream1 = new bytearrayoutputstream(); thumbnail2.compress(bitmap.compressformat.jpeg, 90, stream1); bitmap thumbnail3 = (bitmapfactory.decodefile(selectedimagepath3)); bytearrayoutputstream stream2 = new bytearrayoutputstream(); thumbnail2.compress(bitmap.compressformat.jpeg, 90, stream2); bitmap thumbnail4 = (bitmapfactory.decodefile(selectedimagepath4)); bytearrayoutputstream stream3 = new bytearrayoutputstream(); thumbnail4.compress(bitmap.compressformat.jpeg, 90, stream3); imageview1.setimagebitmap(thumbnail1); thumbnail1.recycle(); imageview2.setimagebitmap(thumbnail2); imageview3.setimagebitmap(thumbnail3 ); imageview4.setimagebitmap(thumbnail4);
it happens because of image size. have resize or compress images. below code that. use per needed:
public static bitmap loadresizedbitmap( string filename, int width, int height, boolean exact ) { bitmap bitmap = null; bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile( filename, options ); if ( options.outheight > 0 && options.outwidth > 0 ) { options.injustdecodebounds = false; options.insamplesize = 2; while ( options.outwidth / options.insamplesize > width && options.outheight / options.insamplesize > height ) { options.insamplesize++; } options.insamplesize--; bitmap = bitmapfactory.decodefile( filename, options ); if ( bitmap != null && exact ) { bitmap = bitmap.createscaledbitmap( bitmap, width, height, false ); } } return bitmap; }
or
public static int calculateinsamplesize( bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { final int halfheight = height / 2; final int halfwidth = width / 2; // calculate largest insamplesize value power of 2 , keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } } return insamplesize; }
Comments
Post a Comment