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

fmms 12年前發布 | 58K 次閱讀 Android Android開發 移動開發

首先,讓我們來看看如何將手機系統圖庫集成到你的App中,然后再從圖庫中選擇圖片來做一些事。例如,在非死book的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,然后替換成下面的代碼:

<?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">
    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"></ImageView>
    <Button
        android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Load Picture"
        android:layout_gravity="center"></Button>
</LinearLayout>

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

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

Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

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

步驟4:獲取選中的圖片

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

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
         Uri selectedImage = data.getData();
         String[] filePathColumn = { MediaStore.Images.Media.DATA };

         Cursor cursor = getContentResolver().query(selectedImage,
                 filePathColumn, null, null, null);
         cursor.moveToFirst();

         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         String picturePath = cursor.getString(columnIndex);
         cursor.close();

         // String picturePath contains the path of selected Image
 }

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

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

package net.viralpatel.android.imagegalleray;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ImageGalleryDemoActivity extends Activity {

    private static int RESULT_LOAD_IMAGE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }
}

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

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

用戶從圖庫選擇圖片

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

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

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

 

源代碼:ImageGalleryDemo.zip (46 KB)

 

原文鏈接

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!