Achieve the ability to call the Android phone's camera

Source: Internet
Author: User

Very simple, just go to the example.

1 XML

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context=". Mainactivity " >Ten  One     <Button A         Android:id= "@+id/btntakepic" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "Take pictures" /> -  -     <ImageView -         Android:id= "@+id/imgview" + Android:layout_width= "100DP" - Android:layout_height= "100DP" + Android:layout_alignleft= "@+id/btntakepic" A Android:layout_below= "@+id/btntakepic" at Android:layout_margintop= "38DP" /> -  - </Relativelayout>

2 Java code

1  PackageCom.example.takepictrures;2 3 ImportJava.io.ByteArrayOutputStream;4 ImportJava.io.File;5 Importjava.io.IOException;6 ImportJava.text.SimpleDateFormat;7 Importjava.util.Date;8 ImportCom.example.util.saveToSdCard;9 ImportAndroid.net.Uri;Ten ImportAndroid.os.Bundle; One Importandroid.os.Environment; A ImportAndroid.provider.MediaStore; - ImportAndroid.provider.MediaStore.Audio.Media; - Importandroid.app.Activity; the Importandroid.content.ContentValues; - Importandroid.content.Intent; - ImportAndroid.graphics.Bitmap; - Importandroid.graphics.BitmapFactory; + ImportAndroid.view.Menu; - ImportAndroid.view.View; + ImportAndroid.view.View.OnClickListener; A ImportAndroid.widget.Button; at ImportAndroid.widget.ImageView; -  -  Public classMainactivityextendsActivityImplementsOnclicklistener { -  -      Publicbutton button; -     Private Static Final intPhoto_graph = 1;// ???? in      PublicImageView ImageView; -  to @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); *ImageView = (ImageView) This. Findviewbyid (R.id.imgview); $Button = (Button) This. Findviewbyid (r.id.btntakepic);Panax NotoginsengButton.setonclicklistener ( This); -  the     } +  A @Override the      Public BooleanOncreateoptionsmenu (Menu menu) { +         //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); $         return true; $     } -  - @Override the      Public voidOnClick (view view) { -         Switch(View.getid ()) {Wuyi          CaseR.id.btntakepic: theIntent Intent =NewIntent (mediastore.action_image_capture); - Startactivityforresult (Intent, photo_graph); Wu              Break; -  About         default: $              Break; -         } -     } -  A @Override +     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { the         if(ResultCode = =NONE) -             return; $  the         if(Requestcode = =photo_graph) { the  theFile picture =NewFile (environment.getexternalstoragedirectory () the+ "/temp.jpg"); -             //Startphotozoom (uri.fromfile (picture)); in         } the  the         if(Data = =NULL) About             return; the  the         //if (Requestcode = = photo_zoom) { the         //Startphotozoom (Data.getdata ()); +         // } -          theString fileName = "";Bayi         if(Requestcode = = 1) { theBundle Extras =Data.getextras (); the             if(Extras! =NULL) { -Bitmap photo = extras.getparcelable ("Data"); -Bytearrayoutputstream stream =NewBytearrayoutputstream (); thePhoto.compress (Bitmap.CompressFormat.JPEG, (), stream);//(0-100)?????? the                  
It's OK to save to SD card here. the Try { theFileName =savetosdcard.getfilename (); - savetosdcard.savemybitmap (photo, fileName); the}Catch(IOException e) { the //TODO auto-generated Catch block the e.printstacktrace ();94 } the imageview.setimagebitmap (photo); the } the }98 Super. Onactivityresult (Requestcode, ResultCode, data); About } - 101 102 //Public void Startphotozoom (Uri uri) {103 //Intent Intent = new Intent ("Com.android.camera.action.CROP");104 //Intent.setdataandtype (URI, image_unspecified); the //Intent.putextra ("Crop", "true");106 // //Aspectx aspecty107 //Intent.putextra ("Aspectx", 1);108 //Intent.putextra ("Aspecty", 1);109 // //OUTPUTX outputy the //Intent.putextra ("Outputx");111 //Intent.putextra ("Outputy"); the //Intent.putextra ("Return-data", true);113 //Startactivityforresult (Intent, photo_resoult); the // } the the}

Achieve the ability to call the Android phone's camera

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.