android - png downloaded size different to server size? -
my app downloads .png files sd card later use. kept getting outofmemoryerrors (if explain too, that'd great!) , took @ sizes of images saved sd card, , seem double on server. why this, , how can make them smaller?
public void oncreate(bundle saved) { setcontentview(r.layout.namedrxnscreen); textview t1 = (textview)findviewbyid(r.id.rxn_text1); textview t2 = (textview)findviewbyid(r.id.rxn_text2); textview t3 = (textview)findviewbyid(r.id.rxn_text3); textview t4 = (textview)findviewbyid(r.id.rxn_text4); iv = (imageview) findviewbyid(r.id.rxn_image); pb = (progressbar) findviewbyid(r.id.rxn_loading); vs = (viewswitcher) findviewbyid(r.id.rxn_switch); try { super.oncreate(saved); [ boring stuff here ] bitmapdrawable image = getimage(c.getstring(5)); if (image != null) { iv.setimagedrawable(getimage(c.getstring(5))); iv.setbackgroundcolor(color.white); vs.setdisplayedchild(1); } else { new downloadtask().execute(c.getstring(5)); } } catch (arrayindexoutofboundsexception ea) { error = "bah!"; showdialog(1); } catch (exception ex) { error = ex.getmessage(); showdialog(1); } } protected class downloadtask extends asynctask<string, integer, bitmap> { @override protected bitmap doinbackground(string... string) { bitmap d = null; try { defaulthttpclient dhc = new defaulthttpclient(); httpget request = new httpget(http_base + string[0] + ".png"); httpresponse response = dhc.execute(request); bufferedinputstream webstream = new bufferedinputstream(response.getentity().getcontent()); d = writetosd(string[0], webstream, d); } catch (exception ex) { errorcatch(ex.getmessage()); } return d; } private bitmap writetosd(string string, bufferedinputstream webstream, bitmap d) { try { webstream.mark(3); webstream.reset(); file f = new file(environment.getexternalstoragedirectory() + sd_dir); f.mkdirs(); file f2 = new file(f, string + ".png"); f2.createnewfile(); bufferedoutputstream fos = new bufferedoutputstream(new fileoutputstream(f2)); int len; byte[] buffer = new byte[1024]; while ((len = webstream.read(buffer)) > 0) { fos.write(buffer, 0, len); } webstream.close(); //fos.flush(); fos.close(); fileinputstream fis = new fileinputstream(new file(f, string + ".png")); d = bitmapfactory.decodestream(fis); return d; } catch (exception ex) { errorcatch(ex.getmessage()); return null; } } protected void onpostexecute(bitmap result) { if (result != null) { iv.setimagebitmap(result); iv.setbackgroundcolor(color.white); vs.setdisplayedchild(1); } } } protected bitmapdrawable getimage(string name) { if (environment.getexternalstoragestate().equals(environment.media_mounted)) { try { //gets sd card file, whacks in stream file f = new file(environment.getexternalstoragedirectory() + sd_dir, name + ".png"); if (f.exists()) { inputstream s = new fileinputstream(f); try { //return decoded bitmap bitmapdrawable d = new bitmapdrawable(getresources(), bitmapfactory.decodestream(s)); //gives outofmemoryerror if png > ~30kb return d; } catch (outofmemoryerror ex) { toast.maketext(this, "an outofmemoryerror occured and" + " image loaded @ lower quality.", toast.length_short).show(); bitmapfactory.options moptions = new bitmapfactory.options(); moptions.insamplesize = 4; bitmapdrawable d = new bitmapdrawable(getresources(), bitmapfactory.decodestream(s, null, moptions)); return d; } } else return null; } catch (exception ex) { return null; } } else { toast.maketext(this, "external storage not available. downloading internet.", toast.length_short).show(); return null; } }
the picture downloaded server done byte per byte , cannot double sized magically...
Comments
Post a Comment