This article is an example of how Android calls the phone's camera function. Share to everyone for your reference. Specifically as follows:
First, main.xml layout file:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent" >
<imageview android:id= "@+id/imageview"
android:adjustviewbounds= "true"
android:layout_gravity= "center"
android:minwidth= "150dip"
android:minheight= "150dip"
android: Layout_width= "Wrap_content"
android:layout_height= "wrap_content"/> <button android:id=
"@+id/" Btnphone "
android:layout_width=" fill_parent "
android:layout_height=" wrap_content "
android:text=" "Albums"/>
<button android:id= "@+id/btntakepicture"
android:layout_height= "Wrap_content
" Android:layout_width= "Fill_parent"
android:text= "photo"/>
</LinearLayout>
Second, the core code:
Package com.ljq.test;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
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.view.View;
Import Android.widget.Button;
Import Android.widget.ImageView;
public class Testactivity extends activity {private static final int NONE = 0; private static final int photo_graph = 1;//camera private static final int photo_zoom = 2;
Scaling private static final int photo_resoult = 3;//result private static final String image_unspecified = "image/*";
Private ImageView ImageView = null;
Private Button btnphone = null;
Private Button btntakepicture = null;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
ImageView = (ImageView) Findviewbyid (R.id.imageview); Btnphone = (Button) Findviewbyid (R.id.btnphone);
Btnphone.setonclicklistener (Onclicklistener);
Btntakepicture = (Button) Findviewbyid (r.id.btntakepicture);
Btntakepicture.setonclicklistener (Onclicklistener); Private final View.onclicklistener Onclicklistener = new View.onclicklistener () {@Override public void OnClick (Vie
W v) {if (V==btnphone) {///Get Picture from album Intent Intent = new Intent (Intent.action_pick, NULL);
Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image_unspecified);
Startactivityforresult (Intent, photo_zoom);
}else if (v==btntakepicture) {//Get picture from photo Intent Intent = new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri.fromfile, new File (environment. getExternalStorageDirectory (), "
Temp.jpg "));
Startactivityforresult (Intent, photo_graph);
}
}
}; @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = NONE) re
Turn Photo if (Requestcode = = PhotO_graph) {//settings file save path file picture = new file (environment.getexternalstoragedirectory () + "/temp.jpg");
Startphotozoom (Uri.fromfile (picture));
} if (data = null) return;
Read Photo album Zoom picture if (Requestcode = = photo_zoom) {startphotozoom (Data.getdata ());
}//Processing result if (Requestcode = = photo_resoult) {Bundle extras = Data.getextras ();
if (extras!= null) {Bitmap photo = extras.getparcelable ("Data");
Bytearrayoutputstream stream = new Bytearrayoutputstream (); Photo.compress (Bitmap.CompressFormat.JPEG, N, stream);//(0-100) compressed file//Here you can save Bitmap to sd card IMAGEVIEW.SETIMAGEBITMA P (photo);
Display the picture on the ImageView control} super.onactivityresult (Requestcode, ResultCode, data); /** * Shrink Picture * * @param uri/public void Startphotozoom (Uri uri) {Intent Intent = new Intent ("Com.android.
Camera.action.CROP ");
Intent.setdataandtype (URI, image_unspecified);
Intent.putextra ("Crop", "true"); Aspectx Aspecty is the ratio of wide to high intent.putextrA ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Outputx Outputy is a cropped picture of wide-high Intent.putextra ("Outputx", 300);
Intent.putextra ("Outputy", 500);
Intent.putextra ("Return-data", true);
Startactivityforresult (Intent, Photo_resoult);
}
}
I hope this article will help you with your Android program.