---
title: "【转】最新历史版本 :【Android】本地图片选择(打开媒体库，选择图片)"
author: "radio123"
date: "2012-06-08T15:00:30Z"
tags: ["图片选择器"]
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-06-15T11:44:07Z"
id: 1555441
url: "https://radio123.iteye.com/blog/1555441"
---

转自： [http://dev.10086.cn/cmdn/wiki/index.php?edition-view-6362-1.html](http://dev.10086.cn/cmdn/wiki/index.php?edition-view-6362-1.html)

我要实现的是：点击Pictures按钮后，获取手机内所有图片，选择某一个图片，并显示到ImageView中。

应用范围： 图片上传时的图片选择  ， 类似"浏览"。

所有的图片都会列出来，包括目录。

在Activity Action里面有一个“ACTION\_GET\_CONTENT”字符串常量，该常量让用户选择特定类型的数据，并返回该数据的URI.我们利用该常量，然后设置类型为“image/\*”，就可获得android手机内的所有image。

 

 

main.xml ：

view plaincopy to clipboardprint?  
&lt;?xml version="1.0" encoding="utf-8"?&gt;   
&lt;LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"   
    android:orientation="vertical"   
    android:layout\_width="fill\_parent"   
    android:layout\_height="fill\_parent"   
    &gt;   
&lt;TextView     
    android:layout\_width="fill\_parent"    
    android:layout\_height="wrap\_content"    
    android:text="@string/hello"   
    /&gt;   
   &lt;Button    
        android:id="@+id/b01"   
        android:layout\_width="fill\_parent"    
        android:layout\_height="wrap\_content"    
    /&gt;   
    &lt;ImageView   
        android:id="@+id/iv01"   
        android:layout\_width="fill\_parent"    
        android:layout\_height="wrap\_content"    
     /&gt;   
&lt;/LinearLayout&gt;   
 

 

Lesson\_01\_Pic.java:

 

view plaincopy to clipboardprint?  
package com.yfz;   
import java.io.FileNotFoundException;   
import android.app.Activity;   
import android.content.ContentResolver;   
import android.content.Intent;   
import android.graphics.Bitmap;   
import android.graphics.BitmapFactory;   
import android.net.Uri;   
import android.os.Bundle;   
import android.util.Log;   
import android.view.View;   
import android.widget.Button;   
import android.widget.ImageView;   
public class Lesson\_01\_Pic extends Activity {   
    /\** Called when the activity is first created. \*/   
    @Override   
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
           
        Button button = (Button)findViewById(R.id.b01);   
        button.setText("选择图片");   
        button.setOnClickListener(new Button.OnClickListener(){   
            @Override   
            public void onClick(View v) {   
                Intent intent = new Intent();   
                /* 开启Pictures画面Type设定为image \*/   
                intent.setType("image/\*");   
                /* 使用Intent.ACTION\_GET\_CONTENT这个Action \*/   
                intent.setAction(Intent.ACTION\_GET\_CONTENT);    
                /* 取得相片后返回本画面 \*/   
                startActivityForResult(intent, 1);   
            }   
               
        });   
    }   
       
    @Override   
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
        if (resultCode == RESULT\_OK) {   
            Uri uri = data.getData();   
            Log.e("uri", uri.toString());   
            ContentResolver cr = this.getContentResolver();   
            try {   
                Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));   
                ImageView imageView = (ImageView) findViewById(R.id.iv01);   
                /* 将Bitmap设定到ImageView \*/   
                imageView.setImageBitmap(bitmap);   
            } catch (FileNotFoundException e) {   
                Log.e("Exception", e.getMessage(),e);   
            }   
        }   
        super.onActivityResult(requestCode, resultCode, data);   
    }   
}   
 

 

好了，就将这么多。

 

 

自己补充：如果想获得选取图片的文件路径、文件名及大小的方法 ，依旧是在onActivityResult ()方法里得到的：

ContentResolver cr = this.getContentResolver();   
             Cursor cursor = cr.query(uri, null, null, null, null);  
             String imgNo = cursor.getString(0);//图片编号  
             String path = cursor.getString(1);//图片文件路径  
             String imgSize = cursor.getString(2);//图片大小  
             String imgName = cursor.getString(3);//图片文件名

 

 //imageView.setImageURI(uri);//也可以直接显示图片