某些设备中的位图更改大小错误
Error on bitmap change size in some Devices
我创建了 activity 可以从图库中挑选照片(有意)并调整挑选照片的大小并放入按钮(在 Drawable 对象中)
Activity一些方法:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case PICK_PHOTO_FOR_AVATAR:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
try{
if(density>=4) {//xxxdpi
setButtonIcon(Photos.changeSize(yourSelectedImage, 196, 196), tempButton);
}else if (density < 4 && density >= 3) {//xxdpi
setButtonIcon(Photos.changeSize(yourSelectedImage, 180, 180), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 3 && density >= 2) {//xdpi
setButtonIcon(Photos.changeSize(yourSelectedImage,96,96), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 2 && density >= 1.5) {//hdpi
setButtonIcon(Photos.changeSize(yourSelectedImage,72,72), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 1.5 && density >= 1) {//mdpi
setButtonIcon(Photos.changeSize(yourSelectedImage,48,48), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 1 && density >= 0.75) {//ldpi
setButtonIcon(Photos.changeSize(yourSelectedImage,36,36), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(context,density+" "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
break;
}
}
private void setButtonIcon(Bitmap icon,DocumentButton btn) {
Drawable draw=new BitmapDrawable(getResources(),icon);
btn.setCompoundDrawablesWithIntrinsicBounds(null, draw , null, null);
}
它可以在某些设备上工作,但是当我在三星 Galaxy S6(密度为 4.0)中进行测试时,出现了这个错误
Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()
当我想调整位图照片的大小时出现此错误:
public static Bitmap changeSize(Bitmap bitmap,int newWidth,int newHeight)throws Exception{
return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
}
我该如何解决这个问题?
我解决了我的问题,我只更改了一些代码行,我认为 Cursor 是我的问题,在 android 5 和更高版本中,Cursor 无法获取图像路径(路径结果 = null)
更新行:
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
我创建了 activity 可以从图库中挑选照片(有意)并调整挑选照片的大小并放入按钮(在 Drawable 对象中)
Activity一些方法:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case PICK_PHOTO_FOR_AVATAR:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
try{
if(density>=4) {//xxxdpi
setButtonIcon(Photos.changeSize(yourSelectedImage, 196, 196), tempButton);
}else if (density < 4 && density >= 3) {//xxdpi
setButtonIcon(Photos.changeSize(yourSelectedImage, 180, 180), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 3 && density >= 2) {//xdpi
setButtonIcon(Photos.changeSize(yourSelectedImage,96,96), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 2 && density >= 1.5) {//hdpi
setButtonIcon(Photos.changeSize(yourSelectedImage,72,72), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 1.5 && density >= 1) {//mdpi
setButtonIcon(Photos.changeSize(yourSelectedImage,48,48), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
} else if (density < 1 && density >= 0.75) {//ldpi
setButtonIcon(Photos.changeSize(yourSelectedImage,36,36), tempButton);
Toast.makeText(context, density + "", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(context,density+" "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
break;
}
}
private void setButtonIcon(Bitmap icon,DocumentButton btn) {
Drawable draw=new BitmapDrawable(getResources(),icon);
btn.setCompoundDrawablesWithIntrinsicBounds(null, draw , null, null);
}
它可以在某些设备上工作,但是当我在三星 Galaxy S6(密度为 4.0)中进行测试时,出现了这个错误
Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()
当我想调整位图照片的大小时出现此错误:
public static Bitmap changeSize(Bitmap bitmap,int newWidth,int newHeight)throws Exception{
return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
}
我该如何解决这个问题?
我解决了我的问题,我只更改了一些代码行,我认为 Cursor 是我的问题,在 android 5 和更高版本中,Cursor 无法获取图像路径(路径结果 = null)
更新行:
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);