java - How do I add a picture from the camera to an ImageView in android? -
my java class code has button lets user access camera, take picture, , picture added imageview.
have failed achieve functionality.
here java code:
package com.example.vikingpianomovers; import java.io.file; import java.io.ioexception; import java.sql.date; import java.text.simpledateformat; import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.net.uri; import android.os.bundle; import android.os.environment; import android.provider.mediastore; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.linearlayout; import android.widget.spinner; public class quoteformactivity extends activity { string mcurrentphotopath; protected static final int media_type_image = 1111; protected static final string image_directory_name = null; private static final int result_ok = 1; private static final int request_image_capture = 0; private static final int request_take_photo = 0; imageview mimageview = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_quote_form); final edittext name = (edittext) findviewbyid(r.id.edittextname); final edittext emailaddress = (edittext) findviewbyid(r.id.edittextemail); final edittext phonenumber = (edittext) findviewbyid(r.id.editphonenumber); final edittext makemodel = (edittext) findviewbyid(r.id.edittextmakemodel); final edittext pianosize = (edittext) findviewbyid(r.id.edittextpianosize); final spinner measurementunit = (spinner) findviewbyid(r.id.spinner2); final edittext cityorigin = (edittext) findviewbyid(r.id.edittextcityorigin); final edittext citydest = (edittext) findviewbyid(r.id.edittextcitydest); final edittext stairs = (edittext) findviewbyid(r.id.edittextstairs); final imageview mimageview = (imageview) findviewbyid(r.id.attachedimage1); final imageview mimageview2 = (imageview) findviewbyid(r.id.attachedimage2); //button starts camera button addpic = (button) findviewbyid(r.id.attachpicbutton); addpic.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { //button click invokes method start camera dispatchtakepictureintent(); } }); //button sends data form user's email button email = (button) findviewbyid(r.id.submitbutton); email.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //intent send email intent email = new intent(android.content.intent.action_send); email.settype("plain/text"); email.putextra(android.content.intent.extra_email, new string[]{"vikingpianomovers@yahoo.com"}); email.putextra(android.content.intent.extra_subject, "viking android app submission"); email.putextra(android.content.intent.extra_text, "name: " + name.gettext().tostring() + '\n' + '\n' + "email address: " + emailaddress.gettext().tostring()+ '\n' + '\n' + "phone number: " + phonenumber.gettext().tostring() + '\n' + '\n' + "make , model: " + makemodel.gettext().tostring() + '\n' + '\n' + "piano size: " + pianosize.gettext().tostring()+ '\n' + '\n' + "city of origin: " + cityorigin.gettext().tostring() + '\n' + '\n' + "city destination: " + citydest.gettext().tostring() + '\n' + '\n' + "number of stairs: " + stairs.gettext().tostring() + '\n' + '\n' + "pic(s): " + mimageview ); startactivity(intent.createchooser(email, "send mail...")); } }); //layout params linearlayout linearlayout.layoutparams layoutparams= new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); //create new linearlayout linearlayout layout = new linearlayout(this); layout.setorientation(linearlayout.vertical); layout.setlayoutparams(layoutparams); new linearlayout.layoutparams( linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); } @override //result of dispatchtakepictureintent protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_image_capture && resultcode == result_ok) { bundle extras = data.getextras(); bitmap imagebitmap = (bitmap) extras.get("data"); mimageview.setimagebitmap(imagebitmap); } } //method create image file in external directory private file createimagefile() throws ioexception { // create image file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date(0)); string imagefilename = "jpeg_" + timestamp + "_"; file storagedir = environment.getexternalstoragepublicdirectory( environment.directory_pictures); file image = file.createtempfile( imagefilename, /* prefix */ ".jpg", /* suffix */ storagedir /* directory */ ); // save file: path use action_view intents mcurrentphotopath = "file:" + image.getabsolutepath(); return image; } //method containing intent access camera , create image file private void dispatchtakepictureintent() { intent takepictureintent = new intent(mediastore.action_image_capture); // ensure there's camera activity handle intent if (takepictureintent.resolveactivity(getpackagemanager()) != null) { // create file photo should go file photofile = null; try { photofile = createimagefile(); } catch (ioexception ex) { // error occurred while creating file log.i("photofile not created", mcurrentphotopath); } // continue if file created if (photofile != null) { takepictureintent.putextra(mediastore.extra_output, uri.fromfile(photofile)); startactivityforresult(takepictureintent, request_take_photo); } } } public quoteformactivity() { // todo auto-generated constructor stub } } here xml:
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="175dp" android:layout_margintop="2dp" android:croptopadding="false" android:paddingbottom="0dp" android:paddingleft="10dp" android:paddingright="10dp" android:src="@drawable/vikingmobilequotelogo" /> <edittext android:id="@+id/edittextname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/namehint" android:inputtype="textpersonname" android:padding="10dp" > </edittext> <edittext android:id="@+id/edittextemail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/emailhint" android:inputtype="textemailaddress" android:padding="10dp" > </edittext> <edittext android:id="@+id/editphonenumber" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/phonenumberhint" android:inputtype="phone" android:padding="10dp" > </edittext> <edittext android:id="@+id/spinnertextview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="text" android:hint="@string/piano_type" android:textsize="18sp" android:padding="10dp"> </edittext> <edittext android:id="@+id/edittextmakemodel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/piano_make_model_hint" android:inputtype="text" android:padding="10dp" > </edittext> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="0dp" > <edittext android:id="@+id/edittextpianosize" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/piano_size_hint" android:inputtype="number" android:layout_weight="1" android:padding="10dp"> </edittext> <spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="5" android:entries="@array/piano_measurement_type" android:prompt="@string/piano_type_prompt" > </spinner> </linearlayout> <edittext android:id="@+id/edittextcityorigin" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/origin_hint" android:padding="10dp"> </edittext> <edittext android:id="@+id/edittextcitydest" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/dest_hint" android:padding="10dp"> </edittext> <edittext android:id="@+id/edittextstairs" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/stairs_hint" android:padding="10dp"> </edittext> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp"> <textview android:id="@+id/textviewsubmit" android:layout_width="150dp" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/submit_text" android:textsize="18sp" > </textview> <button android:id="@+id/attachpicbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="10dp" android:padding="10dp" android:text="@string/picbutton" > </button> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp"> <imageview android:id="@+id/attachedimage1" android:layout_width="150dp" android:layout_height="150dp" android:src="@drawable/ic_launcher" >" </imageview> <imageview android:id="@+id/attachedimage2" android:layout_width="150dp" android:layout_height="150dp" android:src="@drawable/ic_launcher" > </imageview> </linearlayout> <button android:id="@+id/submitbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/submit_button_text" android:padding="10dp" android:layout_margintop="20dp" android:layout_marginbottom="10dp" > </button>
manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.vikingpianomovers" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="22" /> <application android:allowbackup="true" android:icon="@drawable/viking_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:label="@string/app_name" android:name="quoteformactivity"> <intent-filter > <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> </application> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.camera" /> <uses-permission android:name="android.permission.dial_phone"></uses-permission> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage"/>
use this
@override //result of dispatchtakepictureintent protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_image_capture && resultcode == result_ok) { mimageview.setimagebitmap(new bitmapdrawable(getbasecontext(), getpath(data.getdata(),getbasecontext()))); } } add method
public string getpath(uri uri,context c) { string[] projection = { mediastore.images.media.data }; cursor cursor = c.getcontentresolver().query(uri, projection, null, null, null); if (cursor == null) return null; int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data); cursor.movetofirst(); string s=cursor.getstring(column_index); cursor.close(); return s; } sir, question , answer duplicate of soo many questions on stackoverflow, not know you, if okay it, after answer solves problem not accept , delete question, clean site,after problem has been solved
Comments
Post a Comment