Android 打开相册选择单张图片实现代码
作者:Mars-xq 时间:2023-05-29 17:06:38
Android 打开相册选择单张图片实现代码
以下就是如何在应用中打开图库并选择一张图片进行应用,代码很简单,注释很详细。
实现代码:
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*在Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量,
// 该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量,
//然后设置类型为“image/*”,就可获得Android手机内的所有image。*/
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
//Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivityForResult(intent, RESULT);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
/* 将Bitmap设定到ImageView */
iv8.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(), e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/sinat_31057219/article/details/71213084
标签:Android,相册,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot项目配置context path失效的问题解决
2021-08-02 14:50:21
![](https://img.aspxhome.com/file/2023/9/122949_0s.png)
java代码执行字符串中的逻辑运算方法
2023-11-29 12:13:06
使用Java开发实现OAuth安全认证的应用
2023-07-16 13:55:44
![](https://img.aspxhome.com/file/2023/7/64917_0s.png)
SpringCloud 搭建企业级开发框架之实现多租户多平台短信通知服务(微服务实战)
2022-12-04 13:22:53
android判断手机是否安装地图应用实现跳转到该地图应用
2022-11-30 11:01:45
![](https://img.aspxhome.com/file/2023/3/138573_0s.png)
SpringBoot Jpa分页查询配置方式解析
2023-03-02 10:04:02
JAVA使用动态代理对象进行敏感字过滤代码实例
2021-07-29 13:27:26
c#扫描图片去黑边(扫描仪去黑边)
2022-11-06 11:45:55
Spring Boot 整合 TKMybatis 二次简化持久层代码的实现
2021-06-17 06:21:26
Android RecyclerView自定义上拉和下拉刷新效果
2022-03-17 23:06:00
![](https://img.aspxhome.com/file/2023/0/137460_0s.gif)
Android 中RecycleView实现item的点击事件
2023-10-21 18:59:13
Java基础知识精通块作用域与条件及switch语句
2022-12-21 17:50:07
android monkey自动化测试改为java调用monkeyrunner Api
2023-06-20 11:41:39
![](https://img.aspxhome.com/file/2023/1/139521_0s.jpg)
C#对称加密(AES加密)每次生成的结果都不同的实现思路和代码实例
2022-05-24 21:56:35
Spring源码之循环依赖之三级缓存详解
2021-11-13 09:31:56
![](https://img.aspxhome.com/file/2023/7/62737_0s.jpg)
c#关于JWT跨域身份验证的实现代码
2023-09-12 18:26:02
![](https://img.aspxhome.com/file/2023/7/108927_0s.png)
maven profile动态选择配置文件详解
2023-11-05 22:05:20
![](https://img.aspxhome.com/file/2023/4/64514_0s.png)
C#使用RestClient调用Web API
2022-05-30 04:13:09
使用C语言编写基于TCP协议的Socket通讯程序实例分享
2023-07-05 03:30:03
如何使用Java redis实现发送手机验证码功能
2023-11-26 17:25:00