android 程序中选择图片的代码

来源:互联网 发布:hdgame.min.js 编辑:程序博客网 时间:2024/06/06 08:34

From: http://javalover00000.iteye.com/blog/970735

Java代码  收藏代码
  1. private void openPictures() {  
  2.         Intent intent = new Intent();  
  3.         /*Open the page of select pictures and set the type to image*/  
  4.         intent.setType("image/*");  
  5.         intent.setAction(Intent.ACTION_GET_CONTENT);  
  6.         startActivityForResult(intent, REQ_CODE_PICTURES);  
  7.     }  

 这段代码可以调用手机的图库,浏览图片,选择一张图片之后,会回到当前activity 会调用到 onActivityResult 方法,

返回的是一个图片的Uri

 

Java代码  收藏代码
  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  2.         if(resultCode == RESULT_OK) {  
  3.             switch(requestCode) {  
  4.                 case REQ_CODE_CAMERA:  
  5.                     uploadImage(photoTemp);  
  6.                     break;  
  7.                 case REQ_CODE_PICTURES:  
  8.                     Uri uri = data.getData();  
  9.                     ContentResolver cr = this.getContentResolver();  
  10.                     //get the physical path of the image  
  11.                     Cursor c = cr.query(uri, nullnullnullnull);  
  12.                     c.moveToFirst();  
  13.                     photoTemp = c.getString(c.getColumnIndex("_data"));  
  14.                     uploadImage(photoTemp);  
  15.                     break;  
  16.                 default:  
  17.                     break;  
  18.             };  
  19.         }  
  20.         super.onActivityResult(requestCode, resultCode, data);  
  21.     }  

0 0
原创粉丝点击