【问题标题】:Error on bitmap change size in some Devices某些设备中的位图更改大小错误
【发布时间】:2016-12-29 21:35:07
【问题描述】:

我创建了可以从图库中选择照片(有意图)并调整选择的照片大小并放入按钮的活动(在 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 密度)中测试时,我得到了这个错误

尝试调用虚拟方法'int android.graphics.Bitmap.getWidth()

当我想调整位图照片的大小时出现此错误:

public static Bitmap changeSize(Bitmap bitmap,int newWidth,int newHeight)throws Exception{

    return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);

}

我该如何解决这个问题?

【问题讨论】:

  • 您必须将图像大小调整为特定的高度和宽度或动态?
  • 我无法理解您的问题。你检查所有方法?

标签: android bitmap resize runtime-error


【解决方案1】:

我解决了我的问题,我只更改了几行代码,我认为光标是我的问题,在 android 5 及更高版本中,光标无法获取选择的图像路径(路径结果 = null)

更新的行:

final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);

【讨论】:

    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    相关资源
    最近更新 更多