android從圖庫(gallery)選擇一張圖片

在android編程中,有時我們可能會有這樣的需求,從圖庫裡選擇一張圖片作為頭像

 

這時,我們就需要從我們的應用中去激活系統的圖庫應用,並選擇一張圖片

 

這個接口android已經為我們提供

 

我們先來看一下android圖庫的系統源碼,打開android源碼_home\packages\apps,在裡邊我們找到gallery文件夾,即為圖庫的源碼

 

打開後,我們先打開清單文件,在裡邊找到這樣一段代碼

 

[html]  

<activity android:name="com.android.camera.ImageGallery"  

                android:label="@string/gallery_label"  

                android:configChanges="orientation|keyboardHidden"  

                android:icon="@drawable/ic_launcher_gallery">  

            …….  

            <intent-filter>  

                <action android:name="android.intent.action.PICK" />  

                <category android:name="android.intent.category.DEFAULT" />  

                <data android:mimeType="image/*" />  

                <data android:mimeType="video/*" />  

            </intent-filter>  

          …….  

             

</activity>  

從上邊的意圖過濾器我們可以發現,我們可以通過一個叫android.intent.action.PICK的action來激活圖庫並選擇圖片或是視頻

為瞭知道圖庫應用給我們返回的key值是什麼,我們還需到com.android.camera.ImageGallery類去看一下源碼

 

在src目錄下找到該類並打開,我們在裡邊搜“setResult”關鍵字,我們發現這樣一段代碼

 

[java]  

else {  

            Intent result = new Intent(null, img.fullSizeImageUri());  

            if (myExtras != null && myExtras.getBoolean("return-data")) {  

                // The size of a transaction should be below 100K.  

                Bitmap bitmap = img.fullSizeBitmap(  

                        IImage.UNCONSTRAINED, 100 * 1024);  

                if (bitmap != null) {  

                    result.putExtra("data", bitmap);  

                }  

            }  

            setResult(RESULT_OK, result);  

            finish();  

我們發現,圖庫應用會將圖片的URL路徑和圖片縮略圖返回給我們的應用

根據上邊的代碼,我們就可以來編寫實現從圖庫獲取圖片的功能瞭

 

[java]  

public class MainActivity extends Activity {  

    private ImageView iv_image;  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

        this.iv_image = (ImageView) this.findViewById(R.id.iv_image);  

    }  

  

    public void load(View view) {  

        // 激活系統圖庫,選擇一張圖片  

        Intent intent = new Intent();  

        intent.setAction(Intent.ACTION_PICK);  

        intent.setType("image/*");  

        startActivityForResult(intent, 0);  

    }  

  

    @Override  

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

        if (data != null) {  

            // 得到圖片的全路徑  

            Uri uri = data.getData();  

            // 通過路徑加載圖片  

            //這裡省去瞭圖片縮放操作,如果圖片過大,可能會導致內存泄漏  

            //圖片縮放的實現,請看:https://blog.csdn.net/reality_jie_blog/article/details/16891095  

            this.iv_image.setImageURI(uri);  

  

            // 獲取圖片的縮略圖,可能為空!  

            // Bitmap bitmap = data.getParcelableExtra("data");  

            // this.iv_image.setImageBitmap(bitmap);  

  

        }  

        super.onActivityResult(requestCode, resultCode, data);  

    }  

}  

簡單的佈局文件

[html] 

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  

    xmlns:tools="https://schemas.android.com/tools"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent"  

    android:orientation="vertical"  

    tools:context=".MainActivity" >  

  

    <Button  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:onClick="load"  

        android:text="獲取圖庫圖片" />  

  

    <ImageView  

        android:id="@+id/iv_image"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content" />  

  

</LinearLayout>  

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *