protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CAPTURE: Glide.with(AddPaizhaoAct.this).load(imagePath).into(ivAddXinwenHome); Log.e("----imagePath----", imagePath + ""); break; default: break; } }
时间: 2023-06-27 17:08:05 浏览: 32
这是一个 Android 中 startActivityForResult 方法的回调函数,用于获取由其他 Activity 返回的结果。在这个方法中,根据 requestCode 判断是哪个 Activity 返回的结果,然后根据 resultCode 判断返回的结果是否成功,最后根据 data 获取返回的数据。在这段代码中,如果 requestCode 等于 REQUEST_CAPTURE,即拍照的请求码,那么就会使用 Glide 库加载 imagePath 图片路径所对应的图片,并将其设置到 ivAddXinwenHome ImageView 中进行显示。同时,也会将 imagePath 打印输出到日志中。如果 requestCode 不等于 REQUEST_CAPTURE,即不是拍照的请求码,那么就不做任何处理。
相关问题
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PictureSelector.SELECT_REQUEST_CODE) { if (data != null) { PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT); Glide.with(this).load(pictureBean.getPath()).into(imageHead); path = pictureBean.getPath(); } } }解释一下
这是在 Activity 中处理 PictureSelector 返回结果的方法。当用户从相册中选择一张图片后,PictureSelector 会将选择的图片的信息封装在 Intent 中返回给调用方,并在 onActivityResult() 方法中回调该方法。
在这个方法中,首先调用了父类的 onActivityResult() 方法,以确保系统能够正确处理返回结果。然后判断 requestCode 是否等于 PictureSelector.SELECT_REQUEST_CODE,如果是,则说明这是我们打开相册的请求。接着,判断 data 是否为空,如果不为空,则说明用户已经选择了一张图片,并将其封装在 Intent 中返回。
接下来,从 Intent 中获取 PictureBean 对象,并从中获取图片的本地路径。然后使用 Glide 库将图片加载到 ImageView 中,并将图片路径保存到 path 变量中,以便后续使用。需要注意的是,该方法中的 imageHead 变量是一个 ImageView 控件,用于显示所选图片的预览。
以下代码,请用Bitmap替换Glide来实现同样的功能 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { if (requestCode == REQUEST_IMAGE_CAPTURE) { Bitmap photo = (Bitmap) data.getExtras().get("data"); Uri tempUri = getImageUri(photo); selectedImagePath = tempUri.getPath(); Glide.with(this).load(tempUri).into(imageView); } else if (requestCode == REQUEST_IMAGE_PICK) { Uri selectedImageUri = data.getData(); selectedImagePath = selectedImageUri.getPath(); Glide.with(this).load(selectedImageUri).into(imageView); } } }
以下是使用Bitmap替换Glide的代码实现:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
Uri tempUri = getImageUri(photo);
selectedImagePath = tempUri.getPath();
imageView.setImageBitmap(photo);
} else if (requestCode == REQUEST_IMAGE_PICK) {
Uri selectedImageUri = data.getData();
selectedImagePath = selectedImageUri.getPath();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
在这个代码中,我们通过`setImageBitmap()`方法将Bitmap对象直接设置到ImageView中,而不是使用Glide库加载图片。对于拍摄的照片,我们直接使用`photo`作为Bitmap对象;对于从相册选择的照片,我们使用`MediaStore`来获取Bitmap对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)