我使用两部Android手机进行测试,我尝试做的事情就是在从图库和相机中选择图片后立即将图像(位图)显示为ImageView。
它适用于LG-Nexus 5(API Level 24)或Galaxy Note 4(Level 23)等高级API手机,但它不适用于小米MI 3W(API级别19),因为只有在拍摄照片和选择图像后立即显示为ImageView的空白。
这是我的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
Uri image = imageUri;
getContentResolver().notifyChange(image, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
String path = image.getPath();
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, image);
bitmap = modifyOrientation(bitmap, path);
String decoded = MDSUtilities.encodeBitmapToBase64(bitmap, true);
binding.imageProfile.setImageBitmap(bitmap);
Blurry.with(this).from(bitmap).into(binding.profileBannerImage);
UploadRequest request = new UploadRequest(decoded);
ApiProvider.getSharedInstance().uploadProfileImage(this, request, new ApiProvider.APICompletionGeneral() {
@Override
public void OnSuccess() {
}
@Override
public void OnFailure(Boolean handleError, Integer statusCode, String errorMessage) {
}
});
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
break;
case 2 :
if(requestCode == 2 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
getContentResolver().notifyChange(uri, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
String path = getPath(this, uri);
try {
bitmap = MediaStore.Images.Media.getBitmap(cr, uri);
bitmap = modifyOrientation(bitmap, path);
binding.imageProfile.setImageBitmap(bitmap);
Blurry.with(this).from(bitmap).into(binding.profileBannerImage);
UploadRequest request = new UploadRequest(MDSUtilities.encodeBitmapToBase64(bitmap, true));
ApiProvider.getSharedInstance().uploadProfileImage(this, request, new ApiProvider.APICompletionGeneral() {
@Override
public void OnSuccess() {
}
@Override
public void OnFailure(Boolean handleError, Integer statusCode, String errorMessage) {
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我还使用数据绑定和MVVM架构,我也使用第三方库来显示模糊视图(在23级和24级但不是19级工作正常)。
我还没有在这3部手机以外的其他手机上进行过测试。
有没有办法解决这个问题?我似乎无法在网上找到关于API级别和设置位图的解决方案。 或API级别不是因素?