android - How can I save image taken from camera into internal storage -
i have code take photo , save external storage, want save internal storage, please me... should change save internal storage...
thank you
public class mainactivity extends appcompatactivity { public static final int capture_image_fullsize_activity_request_code = 1777; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button button = (button) findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent("android.media.action.image_capture"); file file = new file(environment.getexternalstoragedirectory()+file.separator + "image.jpg"); intent.putextra(mediastore.extra_output, uri.fromfile(file)); startactivityforresult(intent, capture_image_fullsize_activity_request_code); } }); } protected void onactivityresult(int requestcode, int resultcode, intent data) { //check request code matches ours: if (requestcode == capture_image_fullsize_activity_request_code) { //get our saved file bitmap object: file file = new file(environment.getexternalstoragedirectory() + file.separator + "image.jpg"); bitmap bitmap = decodesampledbitmapfromfile(file.getabsolutepath(), 1000, 700); } } public static bitmap decodesampledbitmapfromfile(string path, int reqwidth, int reqheight) { //first decode injustdecodebounds=true check dimensions final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(path, options); // calculate insamplesize, raw height , width of image final int height = options.outheight; final int width = options.outwidth; options.inpreferredconfig = bitmap.config.rgb_565; int insamplesize = 1; if (height > reqheight) { insamplesize = math.round((float) height / (float) reqheight); } int expectedwidth = width / insamplesize; if (expectedwidth > reqwidth) { //if(math.round((float)width / (float)reqwidth) > insamplesize) // if bigger sampsize.. insamplesize = math.round((float) width / (float) reqwidth); } options.insamplesize = insamplesize; // decode bitmap insamplesize set options.injustdecodebounds = false; return bitmapfactory.decodefile(path, options); } }
you can try code save image internal storage:
fileoutputstream fos; try { fos = openfileoutput(filename, context.mode_private); // can use .jpg yourbitmap.compress(bitmap.compressformat.png, 100, fos); } catch (filenotfoundexception e) { log.e(tag, e.getmessage()); } catch (ioexception e) { log.e(tag, e.getmessage()); } { fos.close(); }
you can have @ this: https://developer.android.com/guide/topics/data/data-storage.html#filesinternal
edit
for saving full size image file internal storage, you'll have change your
file file = new file(environment.getexternalstoragedirectory()+file.separator + "image.jpg");
to
file file = new file(context.getfilesdir(), "image.jpg");
getfilesdir() returns internal directory of app. find more detailed information here: https://developer.android.com/training/basics/data-storage/files.html#writeinternalstorage
Comments
Post a Comment