android 程序中选择图片的代码

private void openPictures() {
		Intent intent = new Intent();
		/*Open the page of select pictures and set the type to image*/
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(intent, REQ_CODE_PICTURES);
	}

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

返回的是一个图片的Uri

 

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if(resultCode == RESULT_OK) {
    		switch(requestCode) {
    			case REQ_CODE_CAMERA:
    				uploadImage(photoTemp);
    				break;
    			case REQ_CODE_PICTURES:
    				Uri uri = data.getData();
    				ContentResolver cr = this.getContentResolver();
    				//get the physical path of the image
    				Cursor c = cr.query(uri, null, null, null, null);
    				c.moveToFirst();
    				photoTemp = c.getString(c.getColumnIndex("_data"));
    				uploadImage(photoTemp);
    				break;
    			default:
    				break;
    		};
    	}
		super.onActivityResult(requestCode, resultCode, data);
	}
 

你可能感兴趣的:(C++,c,android,C#)