Saving bitmap to SdCard Android -
i writng code convert png file bmp , save on sdcard. current code.
fileinputstream in; bufferedinputstream buf; try { in = new fileinputstream("file_path_to_read.png"); buf = new bufferedinputstream(in); byte[] bmaparray= new byte[buf.available()]; buf.read(bmaparray); bitmap bmap = bitmapfactory.decodebytearray(bmaparray, 0, bmaparray.length); //code segment save on file int numbytesbyrow = bmap.getrowbytes() * bmap.getheight(); bytebuffer bytebuffer = bytebuffer.allocate(numbytesbyrow); bmap.copypixelstobuffer(bytebuffer); byte[] bytes = bytebuffer.array(); fileoutputstream fileouputstream = new fileoutputstream("file_path_to_save.bmp"); fileouputstream.write(bytes); fileouputstream.close(); if (in != null) { in.close(); } if (buf != null) { buf.close(); } } catch (exception e) { }
i having problem in saving bmap sdcard. examples found use bmap.compress(). using method can't save bmp. can give example on how save bitmap on sdcard?
edit: can save file .bmp sdcard. won't original size. sugguestions on converting png bmp?
file root = new file(environment.getexternalstoragedirectory() + file.separator + "my_folder" + file.separator); root.mkdirs(); file myfile = new file(root, "abc.jpg"); bitmap bitmap = decodefile(myfile, 800, 600); outputstream out = null; file file = new file(mediastoragedir.getabsolutefile() + "/def.jpg"); try { out = new fileoutputstream(file); bitmap .compress(bitmap.compressformat.jpeg, 80, out); out.flush(); out.close(); bitmap.recycle(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } public static bitmap decodefile(file f, int width, int hight) { try { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f), null, o); // new size want scale final int required_width = width; final int required_hight = hight; // find correct scale value. should power of 2. int scale = 2; while (o.outwidth / scale / 2 >= required_width && o.outheight / scale / 2 >= required_hight) scale *= 2; // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; return bitmapfactory.decodestream(new fileinputstream(f), null, o2); } catch (filenotfoundexception e) { } return null; }
Comments
Post a Comment