capturing images using the built-in camera applicationexplore the built-in features provided by Android, built-in image capture and storage capabilities provide a good entry point for all media features on Android, laying the groundwork for us to work with audio and video in later chapters. here's how to take advantage of the built-in camera app.
Package Com.example;import Java.io.file;import Android.app.activity;import android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.Bundle; Import Android.os.environment;import Android.view.display;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;public Class Mainactivity extends Activity {final static int camera_result = 0;imageview IMV; Button but; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.fragment_main); Findviewbyid ();
But.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {
/* Perform camera built-in camera intent */intent i = new Intent (Android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Startactivityforresult ( I, Camera_result);}});} private void Findviewbyid () {but = (Button) Findviewbyid (r.id.but); IMV = (ImageView) Findviewbyid (r.id.img);} /* When the activity is finished, the photo is displayed */
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data); if (ResultCode = = RESULT_OK) {Bundle bundle = Data.getextras (); Bitmap bmp = (Bitmap) bundle.get ("Data"); Imv.setimagebitmap (BMP);}}
Androidmanifest.xml Configuration
<application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@ String/app_name " android:theme=" @style/apptheme "> <activity android:name=" Com.example.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> </application>
Activity_main.xml Configuration
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" > <I Mageview android:id= "@+id/img" android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "android:layout_alignparenttop=" true "android:layout_centerinparent=" true "/> &L T Button android:id= "@+id/but" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:layout_below=" @id/img "android:layout_centerinparent=" true "android:text=" take_a_pict Ure "/> </RelativeLayout>