最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android中从图库中选取图片实例详解
时间:2017-03-06 编辑:简简单单 来源:一聚教程网
android 从图库中选取图片
在android中,如何从图库gallary中挑选图片呢,其实很简单,步骤如下
1) 设计一个imageview,用来显示图库选出来的图片
代码如下 | 复制代码 |
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > android:id="@+id/imgView" android:layout_width="fill_parent" android:layout_weight="1"android:layout_height="wrap_content"> |
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中,因此代码如下:
代码如下 | 复制代码 |
protectedvoidonActivityResult(intrequestCode,intresultCode, 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();
intcolumnIndex = 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控件中咯
-
上一个: 顺维修怎么样?顺维修app功能介绍
-
下一个: 蜂直播怎么登不上去?蜂直播进不去怎么办
相关文章
- sora软件价格介绍 02-22
- sora官网入口地址一览 02-22
- Sora生成的视频使用教程 02-22
- 《梦幻西游》元宵灯谜线索答案大全2024 02-22
- 《原神》有朋自远方来第一天通关攻略 02-22
- 《苏醒之路》四个结局达成攻略 02-22