Android利用Intent實現(xiàn)讀取圖片操作
本文實例演示如何從圖庫(Gallery)中讀取圖像并用ImageView將它顯示出來,供大家參考,具體內(nèi)容如下
運行本示例前,需要先利用相機模擬拍攝一些圖片到圖庫中。
1、運行截圖

2、主要設(shè)計步驟
(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);
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Intent傳遞對象的兩種方法(Serializable,Parcelable)詳細(xì)介紹
- Android基于Intent實現(xiàn)Activity之間數(shù)據(jù)傳遞的方法
- Android 通過Intent使用Bundle傳遞對象詳細(xì)介紹
- 在Android中通過Intent使用Bundle傳遞對象的使用方法
- Android中Intent傳遞對象的3種方式詳解
- 詳解Android中Intent傳遞對象給Activity的方法
- Android開發(fā)之利用Intent實現(xiàn)數(shù)據(jù)傳遞的方法
- android中intent傳遞list或者對象的方法
- Android系列之Intent傳遞對象的幾種實例方法
- Android 使用Intent傳遞數(shù)據(jù)的實現(xiàn)思路與代碼
- Android編程使用Intent傳遞圖片的方法詳解
相關(guān)文章
Android調(diào)用OpenCV2.4.10實現(xiàn)二維碼區(qū)域定位
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用OpenCV 2.4.10實現(xiàn)二維碼區(qū)域定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Android 線程之自定義帶消息循環(huán)Looper的實例
這篇文章主要介紹了Android 線程之自定義帶消息循環(huán)Looper的實例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android使用自定義PageTransformer實現(xiàn)個性的ViewPager動畫切換效果
這篇文章主要介紹了Android使用自定義PageTransformer實現(xiàn)個性的ViewPager切換動畫,具有很好的參考價值,一起跟隨小編過來看看吧2018-05-05
Android開發(fā)實現(xiàn)實時檢測藍(lán)牙連接狀態(tài)的方法【附源碼下載】
這篇文章主要介紹了Android開發(fā)實現(xiàn)實時檢測藍(lán)牙連接狀態(tài)的方法,涉及Android針對藍(lán)牙連接狀態(tài)的監(jiān)測操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-11-11
Android 創(chuàng)建與解析XML(四)——詳解Pull方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Pull方式,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
Android studio 下JNI編程實例并生成so庫的實現(xiàn)代碼
這篇文章主要介紹了Android studio 下JNI編程實例并生成so庫,需要的朋友可以參考下2017-09-09
Android中TextView動態(tài)設(shè)置縮進距離的方法
項目需求如果在項目中第一行文字需要添加布局的情況我們應(yīng)該怎么做呢,經(jīng)過一番考慮和查找我最終選擇了縮進的方式解決這個問題,這篇文章主要給大家介紹了關(guān)于Android中TextView動態(tài)設(shè)置縮進距離的相關(guān)資料,需要的朋友可以參考下2022-04-04

