获取手机中的图片

来源:互联网 发布:淘宝网数据包下载 编辑:程序博客网 时间:2024/04/19 03:14

 

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

根据网上的博客整理的两个实例


设置类型

private final String IMAGE_TYPE = "image/*";

private final int IMAGE_CODE = 0;   //这里的IMAGE_CODE是自己任意定义的

 

//使用intent调用系统提供的相册功能

Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);    //根据类型来获得文件

getAlbum.setType(IMAGE_TYPE);  //设置要获取的类型

startActivityForResult(getAlbum, IMAGE_CODE);

 

//重写onActivityResult当该Activity关闭的时   回调该方法

@Override

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

    if (resultCode != RESULT_OK) {        //此处的 RESULT_OK 是系统自定义得一个常量

        Log.e(TAG,"ActivityResult resultCode error");

        return;

    }

 

    Bitmap bm = null;

 

    //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口

    ContentResolver resolver = getContentResolver();

 

    //此处的用于判断接收的Activity是不是你想要的那个

    if (requestCode == IMAGE_CODE) {

        try {

            Uri originalUri = data.getData();        //获得图片的uri 

 

            bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);        //显得到bitmap图片

 

这里开始的第二部分,获取图片的路径:

 

            String[] proj = {MediaStore.Images.Media.DATA};

 

            //好像是android多媒体数据库的封装接口,具体的看Android文档

            Cursor cursor = managedQuery(originalUri, proj, null, null, null); 

            //按我个人理解 这个是获得用户选择的图片的索引值

            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            //将光标移至开头 ,这个很重要,不小心很容易引起越界

            cursor.moveToFirst();

            //最后根据索引值获取图片路径

            String path = cursor.getString(column_index);

        }catch (IOException e) {

            Log.e(TAG,e.toString()); 

        }

    }

}


main.xml :

view plaincopy to clipboardprint?
<?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"  
    >  
<TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="@string/hello"  
    />  
   <Button   
        android:id="@+id/b01"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
    />  
    <ImageView  
        android:id="@+id/iv01"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
     />  
</LinearLayout>  
 

 

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);  
    }  
}




原创粉丝点击