java - How to save bitmap to internal storage android with an AsyncTask -
i've been writing app android user in 1 activity can choose image browser. when image's picked it's saved internal storage in order read fragment/layout. saving of bitmap takes plenty of time did in first place put method responsible saving files simple thread like:
new thread(new runnable() { @override public void run() { mfilemanager.saveimage(bitmap, getapplicationcontext()); } }).start();
because of that, when task working in background on thread user navigate through layouts. anyway.. main assumptions of app let user add filters image. in order user has obtain image storage. want put progress bar while saving's not finished.
i've 2 classes. piece of first one:
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == request_image_capture && resultcode == result_ok) { muri = data.getdata(); mfilemanagerasync = new filemanagerasync(); try { mdecodemanager = new bitmapdecodemanager(); bitmap = mdecodemanager.getthumbnail(this, muri, 1000); mrotationmanager = new rotationmanager(); mimageview.setimagebitmap(mrotationmanager.setrotatedimage(bitmap, mfilemanager.getrealpathfromuri(this, muri))); new thread(new runnable() { @override public void run() { mfilemanager.saveimage(bitmap, getapplicationcontext()); } }).start(); } catch (ioexception e) { e.printstacktrace(); } } }
second 1 kind of manager saving , loading files:
public class filemanager { private static final string tag = filemanager.class.getname(); private pathmanager mpathmanager = pathmanager.getinstance(); private file mdirectory; private file mpath; private file mfilein; private fileoutputstream mfileout; public filemanager() { log.i(tag, "filemanager() created"); initialize(); } private void initialize() { mdirectory = null; mpath = null; mfilein = null; mfileout = null; } public string saveimage(bitmap bitmap, context context) { log.i(tag, "saveimage()"); contextwrapper contextwrapper = new contextwrapper(context); mdirectory = contextwrapper.getdir("araksimages", context.mode_private); mpath = new file(mdirectory, "picture.jpg"); try { mfileout = new fileoutputstream(mpath); bitmap.compress(bitmap.compressformat.png, 100, mfileout); log.i(tag, "saveimage() - saved in -> " + mpath); mpathmanager.path = mpath.getpath(); log.i(tag, "savedimage() singleton getpath() -> " + mpathmanager.path); mpathmanager.isfileloaded = true; } catch (exception e) { e.printstacktrace(); log.i(tag, "saveimage() - went wrong"); mpathmanager.isfileloaded = false; } { try { mfileout.close(); } catch (ioexception e) { e.printstacktrace(); } } return mdirectory.getabsolutepath(); } public bitmap loadimage(string path) { log.i(tag, "loadimage()"); try { mfilein = new file(path); bitmap bitmap = bitmapfactory.decodestream(new fileinputstream(mfilein)); log.i(tag, "loadimage() - got it!"); return bitmap; } catch (filenotfoundexception e) { e.printstacktrace(); log.i(tag, "loadimage() - went wrong"); } return null; } public string getrealpathfromuri(context context, uri contenturi) { string result; cursor cursor = context.getcontentresolver().query(contenturi, null, null, null, null); if (cursor == null) { result = contenturi.getpath(); } else { cursor.movetofirst(); int idx = cursor.getcolumnindex(mediastore.images.imagecolumns.data); result = cursor.getstring(idx); cursor.close(); } return result; }
i've made research before async tasks , progress bar. i'm sure need 3 overriden methods extended class asynctask pre, doinbackground , post in order put there progress bar. question: what's best way that? how pass arguments such context , bitmap asynctask doinbackground needed in filemanager save image? have extend filemanager async task? or logic , async task should split somehow. i'll appreciate every suggestion. thank you.
edit: i've found similar solution problem. how i've made it. first change connected in main activity class in method 'onactivityresults' i've throw away simple thread , switch async task.
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == request_image_capture && resultcode == result_ok) { muri = data.getdata(); try { mdecodemanager = new bitmapdecodemanager(); bitmap = mdecodemanager.getthumbnail(this, muri, 1000); mrotationmanager = new rotationmanager(); mimageview.setimagebitmap(mrotationmanager.setrotatedimage(bitmap, mfilemanager.getrealpathfromuri(this, muri))); new filemanagerasync(bitmap, getapplicationcontext()).execute(); } catch (ioexception e) { e.printstacktrace(); } } }
and added new class filemanager. arguments're passed in constructor.
public class filemanagerasync extends asynctask<void, void, void> { private filemanager mfilemanager; private progressdialog mprogressdialog; private context mcontext; private bitmap mbitmap; public filemanagerasync(bitmap bitmap, context context) { this.mbitmap = bitmap; this.mcontext = context; mfilemanager = new filemanager(); } @override protected void onpreexecute() { /* mprogressdialog = new progressdialog(mcontext); mprogressdialog.setmessage("please wait"); mprogressdialog.setprogressstyle(progressdialog.style_horizontal); mprogressdialog.setindeterminate(true); mprogressdialog.setcancelable(false); mprogressdialog.show(); */ } @override protected void doinbackground(void... params) { mfilemanager.saveimage(mbitmap, mcontext); return null; } @override protected void onpostexecute(void result) { /* mprogressdialog.dismiss(); mprogressdialog = null; */ }
}
i've commented progress dialog because haven't figure out yet how pass activity. anyway main problem passing 2 arguments solved. thank you.
Comments
Post a Comment