在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"    ><ImageViewandroid: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控件中咯

更多相关文章

  1. 一个Demo让你掌握所有的android控件
  2. Android使用libjpeg实现图片压缩
  3. android访问网络,下载图片,并设置UI
  4. Android Retrofit与Spring后台配合,实现单张、多张图片上传功能
  5. Android 强大的图片加载缓存— Glide
  6. 让 Android 可以识别BMP图片文件,且目前Android所支持的所有图片
  7. Android中自定义控件
  8. android 使用异步加载图片

随机推荐

  1. Android开发环境的搭建步骤
  2. Android(安卓)Studio(十一):代码混淆及打
  3. android音频、视频、拍照基础操作
  4. android 中遥控器键值的添加和修改
  5. android通知栏消息
  6. Android开发:Android快速开发不可或缺的11
  7. 复习TextView(查漏补缺)
  8. 移植rtmpdump(librtmp)到android
  9. Java/Android引用类型及其使用分析
  10. Android保存图片到系统相册