发布于 2016-07-02 03:37:35 | 172 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android利用Intent实现读取图片操作的相关资料,感兴趣的小伙伴们可以参考一下

本文实例演示如何从图库(Gallery)中读取图像并用ImageView将它显示出来,供大家参考,具体内容如下
运行本示例前,需要先利用相机模拟拍摄一些图片到图库中。

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持phperz。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务