如何实现选择头像图标后返回到当前页面代码
时间: 2023-09-23 09:08:40 浏览: 41
好的,以下是一个简单的示例代码,可以在选择头像图标后返回到当前页面并更新头像:
在打开选择头像页面的代码中,使用startActivityForResult()方法打开选择头像页面:
```java
// 打开选择头像页面
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
```
在选择头像页面中,当用户选择头像后,将所选头像的Uri通过Intent返回给调用方:
```java
// 选择头像后返回到调用方页面
Intent resultIntent = new Intent();
resultIntent.setData(imageUri); // 将所选头像的Uri放入Intent中
setResult(Activity.RESULT_OK, resultIntent);
finish(); // 关闭选择头像页面
```
在调用方页面中,重写onActivityResult()方法,接收所选头像的Uri,并更新头像:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri imageUri = data.getData(); // 获取所选头像的Uri
// TODO: 根据所选头像的Uri更新头像
}
}
```
注意,在此示例代码中需要定义一个PICK_IMAGE_REQUEST常量,用于标识发起选择头像请求。具体实现可以参考Android官方文档或相关的开源项目。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)