Category: C #, Android, VS2015;
Date Created: 2016-02-23 I. INTRODUCTION
The example shows how to read an image from the library (Gallery) and display it with ImageView. Second, example-ch1203readgallery
Before running this example, you need to take some pictures to the gallery using the camera simulation.
1. Operation
2. Main design Steps
(1) Add Ch1203_readgallery.axml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"> <ButtonAndroid:id= "@+id/btn1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margin= "30DP"android:layout_gravity= "Center"Android:text= "Pick a picture from the gallery" /> <TextViewAndroid:text= "The picture you picked is:"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/textview1"android:layout_gravity= "Center"Android:layout_margin= "30DP" /> <ImageViewAndroid:id= "@+id/myimageview"android:layout_gravity= "Center"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
(2) Add Ch1203ReadGallery.cs
usingAndroid.app;usingandroid.content;usingAndroid.os;usingAndroid.runtime;usingAndroid.widget;namespacemydemos.srcdemos{[Activity (Label=""Example 12-3" reading a gallery image")] Public classch1203readgallery:activity {protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.ch1203_ReadGallery); varBTN1 = findviewbyid<button>(RESOURCE.ID.BTN1); Btn1. Click+=Delegate { varImageintent =NewIntent (); Imageintent.settype ("image/*"); Imageintent.setaction (intent.actiongetcontent); Startactivityforresult (Intent.createchooser (imageintent,"Select the Picture:"),0); }; } protected Override voidOnactivityresult (intRequestcode, [Generatedenum] Result ResultCode, Intent data) { Base. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode = =Result.ok) {varImageView = findviewbyid<imageview>(Resource.Id.myImageView); Imageview.setimageuri (data. Data); } } }}
Run.
"Android" 12.4 using intent to read pictures from the gallery