xml - How do I display an image that I just took from my camera in my android app? -
i trying take picture , view on screen following code:
public class mainactivity extends actionbaractivity { private static final int picture_request_code = 100; public button camera, gallery; private uri fileuri; private static file mediafile; imageview image; public string filename; @override protected void oncreate(bundle savedinstancestate) { system.out.println("started main activity"); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); camera = (button) findviewbyid(r.id.button1); gallery = (button) findviewbyid(r.id.button2); createlisteners(); } private static uri getoutputmediafile(){ file mediastoragedir = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures), "mustache"); if (! mediastoragedir.exists()){ if (! mediastoragedir.mkdirs()){ log.d("mycameraapp", "failed create directory"); return null; } } string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); mediafile = new file(mediastoragedir.getpath() + file.separator + "img_"+ timestamp + ".jpg"); return uri.fromfile(mediafile); } public void launchcamera(){ intent intent = new intent(mediastore.action_image_capture); fileuri = getoutputmediafile(); // create file save image intent.putextra(mediastore.extra_output, fileuri); startactivityforresult(intent, picture_request_code); } public void launchgallery(){ intent gallery = new intent(intent.action_pick,android.provider.mediastore.images.media.external_content_uri); fileuri = getoutputmediafile(); // create file save image gallery.putextra(mediastore.extra_output, fileuri); startactivityforresult(gallery, picture_request_code); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); system.out.println(string.valueof(requestcode)); system.out.println(string.valueof(resultcode)); system.out.println(data); intent mediascanintent = new intent(intent.action_media_scanner_scan_file); uri contenturi = uri.fromfile(mediafile); mediascanintent.setdata(contenturi); this.sendbroadcast(mediascanintent); } public static final int media_type_video = 2; private void createlisteners() { system.out.println("started createlisteners main activity"); camera.setonclicklistener(new onclicklistener() { public void onclick(view v) { launchcamera(); } }); gallery.setonclicklistener(new onclicklistener() { public void onclick(view v) { launchgallery(); } }); } }
xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="horizontal" tools:context="com.example.mustache.mainactivity" tools:ignore="mergerootframe" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:text="@string/main_camera_button" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:text="@string/main_gallery_button" /> </linearlayout>
however, app takes photo , saves gallery, returns main screen without doing anything. how make display photo?
the problem not displaying captured image.
you need add imageview
in layout
, set captured image in onactivityresult
change onactivityresult
this
public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); uri contenturi = uri.fromfile(mediafile); imageview imageview = (imageview)findviewbyid(r.id.imageview1); imageview.setimageuri(contenturi); }
also, add imageview
in layout
<imageview android:id="@+id/imageview1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1"/>
Comments
Post a Comment