【Android】12.4 利用Intent讀取圖庫中的圖片

來源:互聯網
上載者:User

標籤:

分類:C#、Android、VS2015;

建立日期:2016-02-23 一、簡介

該樣本示範如何從圖庫(Gallery)中讀取映像並用ImageView將它顯示出來。 二、樣本—ch1203ReadGallery

運行本樣本前,需要先利用相機類比拍攝一些圖片到圖庫中。

1、運行

  

2、主要設計步驟

(1)添加ch1203_ReadGallery.axml

<?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">    <Button        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="30dp"        android:layout_gravity="center"        android:text="從圖庫中挑選一幅圖片" />    <TextView        android:text="你挑選的圖片為:"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/textView1"        android:layout_gravity="center"        android:layout_margin="30dp" />    <ImageView        android:id="@+id/myImageView"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

(2)添加ch1203ReadGallery.cs

using Android.App;using Android.Content;using Android.OS;using Android.Runtime;using Android.Widget;namespace MyDemos.SrcDemos{    [Activity(Label = "【例12-3】讀取圖庫圖片")]    public class ch1203ReadGallery : Activity    {        protected override void OnCreate(Bundle savedInstanceState)        {            base.OnCreate(savedInstanceState);            SetContentView(Resource.Layout.ch1203_ReadGallery);            var btn1 = FindViewById<Button>(Resource.Id.btn1);            btn1.Click += delegate {                var imageIntent = new Intent();                imageIntent.SetType("image/*");                imageIntent.SetAction(Intent.ActionGetContent);                StartActivityForResult( Intent.CreateChooser(imageIntent, "選擇的圖片:"), 0);            };        }        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)        {            base.OnActivityResult(requestCode, resultCode, data);            if (resultCode == Result.Ok)            {                var imageView = FindViewById<ImageView>(Resource.Id.myImageView);                imageView.SetImageURI(data.Data);            }        }    }}

運行。

【Android】12.4 利用Intent讀取圖庫中的圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.