选择的图像主要适用于所有手机设备。但是一些用户报告说在选择图像时得到空白/红色图像。我使用下面的代码来选择图像
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
然后,我使用这段代码在 imageview 中显示选取的图像
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
Uri selectedImageURI = data.getData();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
bitmap= ImageDecoder.decodeBitmap(ImageDecoder.createSource(mActivity.getContentResolver(), selectedImageURI));
} else {
bitmap= MediaStore.Images.Media.getBitmap(mActivity.getContentResolver(), selectedImageURI);
}
}catch (Exception e){
e.printStackTrace();
}
Glide.with(activity).load(imageUrl)
.thumbnail(Glide.with(activity).load(R.drawable.loading))
.error(R.drawable.loading)
.override(bitmap.getWidth(),bitmap.getHeight())
.into(imageView);
}
}