0
我在按鈕創建的活動,可以選擇照片從畫廊(意圖),並調整大小挑選照片,並把(在可繪製對象)錯誤的位圖改變大小在某些設備
活動的一些方法:
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密度)我得到這個錯誤
試圖調用虛擬方法INT android.graphics.Bitmap.getWidth()
此錯誤快到時,我想調整的位圖圖片:
public static Bitmap changeSize(Bitmap bitmap,int newWidth,int newHeight)throws Exception{
return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
}
我怎樣才能解決這個問題?
您必須將圖像大小調整爲特定的高度和寬度或動態? –
我無法理解你的問題。你檢查所有的方法? – b4hr4m