java - android take multiple image with camera -
i've made code capturing image using device built-in camera , archiving in database server. each time captures image , views it, make imageview layout dynamically (ex: image ic_launcher).
whenever ic_launcher image clicked, go mainactivity.java page, use of intent, take image.
my question is, how can view of captured images / archived images in uploadactivity.java?
note: image viewed in uploadactivity.java
mainactivity.java
private void captureimage() { intent intent = new intent(mediastore.action_image_capture); fileuri = getoutputmediafileuri(media_type_image); intent.putextra(mediastore.extra_output, fileuri); // start image capture intent startactivityforresult(intent, camera_capture_image_request_code); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); // save file url in bundle null on screen orientation // changes outstate.putparcelable("file_uri", fileuri); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); // file url fileuri = savedinstancestate.getparcelable("file_uri"); } /** * receiving activity result method called after closing camera * */ @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // if result capturing image if (requestcode == camera_capture_image_request_code) { if (resultcode == result_ok) { launchuploadactivity(true); } else if (resultcode == result_canceled) { // user cancelled image capture toast.maketext(getapplicationcontext(), "user cancelled image capture", toast.length_short) .show(); } else { // failed capture image toast.maketext(getapplicationcontext(), "sorry! failed capture image", toast.length_short) .show(); } } } private void launchuploadactivity(boolean isimage){ intent = new intent(mainactivity.this, uploadactivity.class); i.putextra("filepath", fileuri.getpath()); i.putextra("isimage", isimage); startactivity(i); } /** * ------------ helper methods ---------------------- * */ public uri getoutputmediafileuri(int type) { return uri.fromfile(getoutputmediafile(type)); } private static file getoutputmediafile(int type) { // external sdcard location file mediastoragedir = new file( environment .getexternalstoragepublicdirectory(environment.directory_pictures), config.image_directory_name); // create storage directory if not exist if (!mediastoragedir.exists()) { if (!mediastoragedir.mkdirs()) { log.d(tag, "oops! failed create " + config.image_directory_name + " directory"); return null; } } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); file mediafile; mediafile = new file(mediastoragedir.getpath() + file.separator + "img_" + timestamp + ".jpg"); return mediafile; }
uploadactivity.java
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_upload); txtpercentage = (textview) findviewbyid(r.id.txtpercentage); btnupload = (button) findviewbyid(r.id.btnupload); button btntaking = (button)findviewbyid(r.id.btntake); layout2 = (linearlayout) findviewbyid(r.id.ly2); progressbar = (progressbar) findviewbyid(r.id.progressbar); vidpreview = (videoview) findviewbyid(r.id.videopreview); // changing action bar background color getactionbar().setbackgrounddrawable( new colordrawable(color.parsecolor(getresources().getstring( r.color.action_bar)))); // receiving data previous activity intent intent = getintent(); // image or video path captured in previous activity filepath = intent.getstringextra("filepath"); arraylist<string> yourfilepaths = new arraylist<>();// create arraylist // first have check if you've saved filepaths sharedpreferences prefs = getsharedpreferences("savedfilepaths", context.mode_private); string myjsonarraystring = prefs.getstring("savedfilepathsjsonarray", ""); if (!myjsonarraystring.isempty()) { jsonarray jsonarray = null; try { jsonarray = new jsonarray(myjsonarraystring); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } (int = 0; < jsonarray.length(); i++) { try { yourfilepaths.add(jsonarray.get(i).tostring()); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } } // add last photo you've taken yourfilepaths.add(filepath); // save new arraylist in sharedpreferences: jsonarray mjsonarray = new jsonarray(yourfilepaths); sharedpreferences.editor editor = prefs.edit(); editor.putstring("savedfilepathsjsonarray", mjsonarray.tostring()); // show images filepaths file imgfile; (int = 0; < yourfilepaths.size(); ++i) { imgfile = new file(yourfilepaths.get(i)); if (imgfile.exists()) { final bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; bitmap mybitmap = bitmapfactory.decodefile(imgfile.getabsolutepath(),options); imageview myimage = new imageview(this); myimage.setimagebitmap(mybitmap);// in step you've // create dynamic imageviews // see more 1 // picture layout2.addview(myimage);// add dynamic imageviews // layout } } btntaking.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(uploadactivity.this, mainactivity.class); startactivity(intent); } }); btnupload.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // uploading file server new uploadfiletoserver().execute(); } }); }
activity_upload
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/view_background" android:orientation="vertical" android:padding="10dp" > <!-- display picture taken --> <linearlayout android:id="@+id/ly2" android:layout_width="wrap_content" android:layout_height="100dp" android:orientation="vertical" > </linearlayout> <textview android:id="@+id/txtpercentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginbottom="15dp" android:layout_margintop="15dp" android:textcolor="@color/txt_font" android:textsize="30dp" /> <progressbar android:id="@+id/progressbar" style="?android:attr/progressbarstylehorizontal" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_marginbottom="35dp" android:visibility="gone" /> <button android:id="@+id/btnupload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginbottom="20dp" android:background="@color/btn_bg" android:paddingleft="20dp" android:paddingright="20dp" android:text="@string/btnuploadtoserver" android:textcolor="@color/white" android:visibility="gone" /> <textview android:id="@+id/idt" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/btntake" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginbottom="20dp" android:background="@color/btn_bg" android:paddingleft="20dp" android:paddingright="20dp" android:text="take picture" android:textcolor="@color/white" />
output
note: got code androidhive
ok think understand problem. understand code check this:
show image view file path in android?
create imageviews dynamically inside loop
last edit: add commit() solve problem
what going try save file paths in arraylist , save array in sharedpreferences:
layout_example.xml <-- create layout on xml add imageviews inside
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:padding="5dp"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/mylinearlayout"></linearlayout> </relativelayout>
uploadactivity.java
override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_upload);//add linearlayout of layout_example.xml txtpercentage = (textview) findviewbyid(r.id.txtpercentage); btnupload = (button) findviewbyid(r.id.btnupload); layout = (linearlayout) findviewbyid(r.id.ly1); progressbar = (progressbar) findviewbyid(r.id.progressbar); imgpreview = (imageview) findviewbyid(r.id.imgpreview); addimageview(layout); // changing action bar background color getactionbar().setbackgrounddrawable( new colordrawable(color.parsecolor(getresources().getstring( r.color.action_bar)))); // receiving data previous activity intent = getintent(); // image or video path captured in previous activity filepath = i.getstringextra("filepath"); linearlayour ly_mylayout = (linearlayout)findviewbyid(r.id.mylinearlayout); arraylist<string> yourfilepaths = new arraylist<>();//create arraylist //first have check if you've saved filepaths sharedpreferences prefs = getsharedpreferences("savedfilepaths", context.mode_private); string myjsonarraystring = prefs.getstring("savedfilepathsjsonarray", ""); if(!myjsonarraystring.isempty()){ jsonarray jsonarray = new jsonarray(myjsonarraystring); (int i=0;i<jsonarray.length();i++){ yourfilepaths.add(jsonarray.get(i).tostring()); } } //add last photo you've taken yourfilepaths.add(filepath); //save new arraylist in sharedpreferences: jsonarray mjsonarray = new jsonarray(yourfilepaths); sharedpreferences.editor editor = prefs.edit(); editor.putstring("savedfilepathsjsonarray",mjsonarray.tostring()).commit(); // add commit //show images filepaths file imgfile; (int = 0; < yourfilepaths.size(); ++i) { imgfile = new file(yourfilepaths.get(i)); if(imgfile.exists()){ bitmap mybitmap = bitmapfactory.decodefile(imgfile.getabsolutepath()); imageview myimage = new imageview(this); myimage.setimagebitmap(mybitmap);// in step you've create dynamic imageviews see more 1 picture ly_mylayout.addview(myimage);//then add dynamic imageviews layout } } btnupload.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // uploading file server new uploadfiletoserver().execute(); } }); } /** * displaying captured image/video on screen * */ private void previewmedia(boolean isimage) { // checking whether captured media image or video if (isimage) { imgpreview.setvisibility(view.visible); vidpreview.setvisibility(view.gone); // bimatp factory bitmapfactory.options options = new bitmapfactory.options(); // down sizing image throws outofmemory exception larger // images options.insamplesize = 8; final bitmap bitmap = bitmapfactory.decodefile(filepath, options); imgpreview.setimagebitmap(bitmap); } } private void addimageview(linearlayout layout) { = 0; imageview imageview = new imageview(this); imageview.setimageresource(r.drawable.ic_launcher); imageview.setid(i); layout.addview(imageview); imageview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(uploadactivity.this, mainactivity.class); startactivity(intent); } }); }
Comments
Post a Comment