某些设备中位图更改大小出错

时间:2016-12-28 11:15:53

标签: android bitmap resize runtime-error

我创建了一个活动,可以从图库中选择照片(有意图)并调整拾取的照片大小并放入按钮(在Drawable对象中)

活动一些方法:

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密度)测试时,我收到此错误

  

尝试调用虚拟方法&#39; int android.graphics.Bitmap.getWidth()

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

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

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

}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,我改变了一些代码行,我认为Cursor是我的问题,在android 5和上层,Cursor无法获取选择的图像路径(path result = null)

更新了行:

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