现在的位置: 首页 > 综合 > 正文

android、获取本地图片|直接获取照相图片

2013年08月12日 ⁄ 综合 ⁄ 共 1892字 ⁄ 字号 评论关闭

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

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

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

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

 


  1. <span style="font-size:18px;">public class Lesson_01_Pic extends Activity {    
  2.     /** Called when the activity is first created. */    
  3.     @Override    
  4.     public void onCreate(Bundle savedInstanceState) {    
  5.         super.onCreate(savedInstanceState);    
  6.         setContentView(R.layout.main);    
  7.             
  8.         Button button = (Button)findViewById(R.id.b01);    
  9.         button.setText("选择图片");    
  10.         button.setOnClickListener(new Button.OnClickListener(){    
  11.             @Override    
  12.             public void onClick(View v) {    
  13.                 Intent intent = new Intent();    
  14.                 /* 开启Pictures画面Type设定为image */    
  15.                 intent.setType("image/*");    
  16.                 /* 使用Intent.ACTION_GET_CONTENT这个Action */    
  17.                 intent.setAction(Intent.ACTION_GET_CONTENT);     
  18.                 /* 取得相片后返回本画面 */    
  19.                 startActivityForResult(intent, 1);    
  20.             }    
  21.                 
  22.         });    
  23.     }    
  24.         
  25.     @Override    
  26.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
  27.         if (resultCode == RESULT_OK) {    
  28.             Uri uri = data.getData();    
  29.             Log.e("uri", uri.toString());    
  30.             ContentResolver cr = this.getContentResolver();    
  31.             try {    
  32.                 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));    
  33.                 ImageView imageView = (ImageView) findViewById(R.id.iv01);    
  34.                 /* 将Bitmap设定到ImageView */    
  35.                 imageView.setImageBitmap(bitmap);    
  36.             } catch (FileNotFoundException e) {    
  37.                 Log.e("Exception", e.getMessage(),e);    

抱歉!评论已关闭.