歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 如何從系統圖庫中選擇圖片

Android 如何從系統圖庫中選擇圖片

日期:2017/3/1 10:23:02   编辑:Linux編程

這幾天我都在做Android的App,同時學習它的API,我將分享一些我學到的東西,比如: 如何從系統圖庫中選擇圖片。

首先,讓我們來看看如何將手機系統圖庫集成到你的App中,然後再從圖庫中選擇圖片來做一些事。例如,在Facebook的App,你就可以直接選擇手機上的圖片上傳到你的個人資料。

讓我們來做一個簡單例子,要求:

  1. 屏幕上顯示一個按鈕和圖片視圖控件。
  2. 點擊“載入圖片”按鈕,將用戶重定向到Android的圖片庫,在那裡可以選擇一個圖片。
  3. 一旦圖片被選中,圖片將在主屏幕上的圖片視圖控件中顯示。

讓我們開始。

步驟1:創建基本的Android項目

在Eclipse中,點擊New > Project > Android Project,給項目取名為“ImageGalleryDemo”,然後選擇Android2.1或sdk 7。

一旦這一步完成,你將看到一個基本的hello world程序。

步驟2:修改布局文件

在我們的例子中,我們需要一個簡單的布局:一個ImageView控件來顯示我們選中的圖片,一個Button控件點擊重定向到手機圖庫。

在項目中打開layout/main.xml,然後替換成下面的代碼:

01 <?xml version="1.0" encoding="utf-8"?> 02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03 android:orientation="vertical" 04 android:layout_width="fill_parent" 05 android:layout_height="fill_parent"> 06 <ImageView 07 android:id="@+id/imgView" 08 android:layout_width="fill_parent" 09 android:layout_height="wrap_content" 10 android:layout_weight="1"></ImageView> 11 <Button 12 android:id="@+id/buttonLoadPicture" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_weight="0" 16 android:text="Load Picture" 17 android:layout_gravity="center"></Button> 18 </LinearLayout>

步驟3:編寫重定向到圖片庫的代碼

現在我們需要寫一些Java代碼來處理按鈕的點擊事件,而重定向到圖片庫的代碼如下:

1 Intent i = new Intent( 2 Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 3 4 startActivityForResult(i, RESULT_LOAD_IMAGE);

注意:這裡要傳一個整形的常量RESULT_LOAD_IMAGEstartActivityForResult()方法。

步驟4:獲取選中的圖片

一旦用戶選擇了一張圖片,onActivityResult()方法將會被調用。我們需要處理這個方法得到的數據,代碼如下:

01 @Override 02 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 03 super.onActivityResult(requestCode, resultCode, data); 04 05 if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 06 Uri selectedImage = data.getData(); 07 String[] filePathColumn = { MediaStore.Images.Media.DATA }; 08 09 Cursor cursor = getContentResolver().query(selectedImage, 10 filePathColumn, null, null, null); 11 cursor.moveToFirst(); 12 13 int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 14 String picturePath = cursor.getString(columnIndex); 15 cursor.close(); 16 17 // String picturePath contains the path of selected Image 18 }

注意:onActivityResult()方法只有當圖片被選中後才會調用。在這個方法中,我們需要檢查requestCode是否是我們之前傳給startActivityForResult()方法的RESULT_LOAD_IMAGE。

最終代碼
ImageGalleryDemoActivity類的最終代碼如下:

01 package net.viralpatel.android.imagegalleray; 02 03 import android.app.Activity; 04 import android.content.Intent; 05 import android.database.Cursor; 06 import android.graphics.BitmapFactory; 07 import android.net.Uri; 08 import android.os.Bundle; 09 import android.provider.MediaStore; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.ImageView; 13 14 public class ImageGalleryDemoActivity extends Activity { 15 16 private static int RESULT_LOAD_IMAGE = 1; 17 18 @Override 19 public void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 23 Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture); 24 buttonLoadImage.setOnClickListener(new View.OnClickListener() { 25 26 @Override 27 public void onClick(View arg0) { 28 29 Intent i = new Intent( 30 Intent.ACTION_PICK, 31 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 32 33 startActivityForResult(i, RESULT_LOAD_IMAGE); 34 } 35 }); 36 } 37 38 @Override 39 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 40 super.onActivityResult(requestCode, resultCode, data); 41 42 if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 43 Uri selectedImage = data.getData(); 44 String[] filePathColumn = { MediaStore.Images.Media.DATA }; 45 46 Cursor cursor = getContentResolver().query(selectedImage, 47 filePathColumn, null, null, null); 48 cursor.moveToFirst(); 49 50 int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 51 String picturePath = cursor.getString(columnIndex); 52 cursor.close(); 53 54 ImageView imageView = (ImageView) findViewById(R.id.imgView); 55 imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 56 57 } 58 59 } 60 }

程序截圖
第一屏:用戶將重定向到手機圖庫

用戶從圖庫選擇圖片

在我們的App顯示用戶選中的圖片

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/5月/12日/Android 如何從系統圖庫中選擇圖片/

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved