android - How to save image bitmap after rotation? -
i develop app save images sd card , pictures upside want rotate them , save them in rotate position choose . know how rotate on code image not saved permanently. here code : //rotate picture
public static bitmap rotate(bitmap source, float angle) { matrix matrix = new matrix(); matrix.postrotate(angle); return bitmap.createbitmap(source, 0, 0, source.getwidth(),source.getheight(), matrix, false); }
//resize image
public void resizeimage(string path , int wdist,int hdist){ try { int inwidth = 0; int inheight = 0; inputstream in = new fileinputstream(path); // decode image size (decode metadata only, not whole image) bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodestream(in, null, options); in.close(); in = null; // save width , height inwidth = options.outwidth; inheight = options.outheight; // decode full image pre-resized in = new fileinputstream(path); options = new bitmapfactory.options(); // calc rought re-size (this no exact resize) options.insamplesize = math.max(inwidth/wdist, inheight/hdist); // decode full image bitmap roughbitmap = bitmapfactory.decodestream(in, null, options); // calc exact destination size matrix m = new matrix(); rectf inrect = new rectf(0, 0, roughbitmap.getwidth(), roughbitmap.getheight()); rectf outrect = new rectf(0, 0, wdist, hdist); m.setrecttorect(inrect, outrect, matrix.scaletofit.center); float[] values = new float[9]; m.getvalues(values); // resize bitmap bitmap resizedbitmap = bitmap.createscaledbitmap(roughbitmap, (int) (roughbitmap.getwidth() * values[0]), (int) (roughbitmap.getheight() * values[4]), true); // save image try { fileoutputstream out = new fileoutputstream(path); resizedbitmap.compress(bitmap.compressformat.jpeg, 80, out); } catch (exception e) { log.e("image", e.getmessage(), e); } } catch (ioexception e) { log.e("image", e.getmessage(), e); } }
thanks helpers :)
try following code.
string root = environment.getexternalstoragedirectory().tostring(); file mydir = new file(root + "/saved_images"); mydir.mkdirs(); random generator = new random(); int n = 10000; n = generator.nextint(n); string fname = "image-"+ n +".jpg"; file file = new file (mydir, fname); if (file.exists ()) file.delete (); try { finalbitmap = rotate(bmp,50); fileoutputstream out = new fileoutputstream(file); finalbitmap.compress(bitmap.compressformat.jpeg, 90, out); out.flush(); out.close(); } catch (exception e) { e.printstacktrace(); }
and dont forgot take below permission
manifest
<uses-permission android:name="android.permission.write_external_storage" />
Comments
Post a Comment