android 从图库中选取图片 在Android中,如何从图库gallary中挑选图片呢,其实很简单,步骤如下 1) 设计一个imageview,用来显示图库选出
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控件中咯
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:Android自定义ImageView实现在图片上添加图层效果Android 实现局部图片滑动指引效果Android ViewPager实现图片轮翻效果Android编程实现支持拖动改变位置的图片中叠加文字功能示例Android中超大图片无法显示的问题解决Android自定义组件获取本地图片和相机拍照图片android自定义ImageView仿图片上传示例Android ViewPager加载图片效果Android百度地图应用之图层展示Android编程实现图片背景渐变切换与图层叠加效果
--结束END--
本文标题: Android中从图库中选取图片实例详解
本文链接: https://www.lsjlt.com/news/22448.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0