android中从图库中选取图片

  在android中,如何从图库gallary中挑选图片呢,其实很简单,步骤如下

1) 设计一个imageview,用来显示图库选出来的图片
  

<?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"
    >
	<ImageView
			android:id="@+id/imgView"
			android:layout_width="fill_parent"
			android:layout_weight="1" android:layout_height="wrap_content"></ImageView>
	<Button 
			android:layout_height="wrap_content" 
			android:text="Load Picture" 
			android:layout_width="wrap_content" 
			android:id="@+id/buttonLoadPicture" 
			android:layout_weight="0" 
			android:layout_gravity="center"></Button>
</LinearLayout>




2) 学习如何在按键中调出gallary,其实也就是intent了,如下
   Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE);


3) 然后在onActivityResult中对调出图库后,选定好的图片,我们要重新显示在页面的imageview中,因此代码如下:
 

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	
		if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			
			ImageView imageView = (ImageView) findViewById(R.id.imgView);
			imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
		
		}



  其中就是Uri selectedImage = data.getData();获得了图库中的图片所有数据了。

  这样一来,当用户在图库中选好图片后,就可以呈现在imageview控件中咯
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值