zoukankan      html  css  js  c++  java
  • 【Android】12.4 利用Intent读取图库中的图片

    分类:C#、Android、VS2015;

    创建日期:2016-02-23

    一、简介

    该示例演示如何从图库(Gallery)中读取图像并用ImageView将它显示出来。

    二、示例—ch1203ReadGallery

    运行本示例前,需要先利用相机模拟拍摄一些图片到图库中。

    1、运行截图

    image   image

    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);
                }
            }
        }
    }

    运行。

  • 相关阅读:
    聚类算法优秀博客链接
    读安晓辉老师的访谈有感
    机器学习概览之常见算法汇总
    稀疏表达和压缩感知的一些对比
    [Linked List]Reverse Linked List,Reverse Linked List II
    [Linked List]Palindrome Linked List
    [Linked List]Remove Nth Node From End of List
    [Tree]Binary Tree Inorder Traversal
    [Tree]Binary Tree Preorder Traversal
    [stack]Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/rainmj/p/5208688.html
Copyright © 2011-2022 走看看