Android Development Call System Gallery and camera

Source: Internet
Author: User

In the development of Android often use the image upload function, commonly used is to call the Android system comes with the library and photo function. The sample code is as follows:

Java code:

public class Testactivity extends Activity {public final static int photo_zoom = 0;    Public final static int take_photo = 1;    Public final static int photo_result = 2;    public static final String image_unspecified = "image/*";    Private String imagedir;private ImageView avatar;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.main);    Avatar = (ImageView) Findviewbyid (R.id.avatar);        This map library selection button LinearLayout upload = (linearlayout) Findviewbyid (R.id.local_select_button);                Upload.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                Intent Intent = new Intent (intent.action_get_content);                Intent.settype (image_unspecified);                Intent wrapperintent=intent.createchooser (Intent, NULL); Startactivityforresult (WrapperinteNT, Photo_zoom);   }        });        Photo button LinearLayout takephoto= (linearlayout) Findviewbyid (R.id.take_photo_button);                Takephoto.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                Imagedir = "Temp.jpg";                Intent intent=new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri.fromfile (New File (environment.getexternalstoragedirect                Ory (), imagedir));            Startactivityforresult (Intent, Take_photo); }        });}        Picture scaling public void Photozoom (Uri uri) {Intent Intent = new Intent ("Com.android.camera.action.CROP");        Intent.setdataandtype (URI, image_unspecified);        Intent.putextra ("Crop", "true");        Aspectx Aspecty is a proportional Intent.putextra ("Aspectx", 1) of the wide height;        Intent.putextra ("Aspecty", 1);        Outputx Outputy is a cropped picture with a wide height of Intent.putextra ("Outputx", 250);Intent.putextra ("Outputy", 250);        Intent.putextra ("Return-data", true);    Startactivityforresult (Intent, Photo_result);  } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = =            RESULT_OK) {if (Requestcode = = photo_zoom) {photozoom (Data.getdata ()); } if (Requestcode = = Take_photo) {file picture = new File (environment.getexternalstoragedirecto                RY () + "/" + Imagedir);            Photozoom (Uri.fromfile (picture));                } if (Requestcode = = photo_result) {Bundle extras = Data.getextras ();                    if (extras! = null) {Bitmap photo = extras.getparcelable ("Data");                    Bytearrayoutputstream stream = new Bytearrayoutputstream ();                    Photo.compress (Bitmap.CompressFormat.JPEG, (), stream);                Avatar.setimagebitmap (photo);          }  }} super.onactivityresult (Requestcode, ResultCode, data);}} 

Layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" #fcf3ef "a        Ndroid:gravity= "Center_horizontal" android:orientation= "vertical" android:paddingtop= "30DP" > <imageview Android:id= "@+id/avatar" android:layout_width= "130DP" android:layout_height= "130DP" Android:back ground= "@drawable/avatar_frame"/> <linearlayout android:layout_width= "Fill_parent" Android:layout_ height= "Wrap_content" android:layout_margintop= "30DP" android:gravity= "Center_horizontal" > <Li Nearlayout android:id= "@+id/local_select_button" android:layout_width= "Wrap_content" Android            oid:layout_height= "wrap_content" android:gravity= "center" android:orientation= "vertical" >             <imageview   Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:backgrou nd= "@drawable/local"/> <textview android:layout_width= "Wrap_content" Andro                id:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:text= "Local selection" Android:textcolor= "#000000" android:textsize= "20sp"/> </LinearLayout> <li Nearlayout android:id= "@+id/take_photo_button" android:layout_width= "Wrap_content" Androi            d:layout_height= "Wrap_content" android:layout_marginleft= "50DP" android:gravity= "center"                android:orientation= "vertical" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:background= "@drawable/take_photo"/> <te Xtview anDroid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_margin top= "10DP" android:text= "photo" android:textcolor= "#000000" android:textsize= "20 SP "/> </LinearLayout></LinearLayout></LinearLayout>

  

The layout file is as follows:

The local selections are as follows:

The camera is photographed as follows:

Image zoom:

Image upload results:

Android Development Call System Gallery and camera

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.