java.lang.IllegalArgumentException:x + width必须是< = bitmap.width()

时间:2015-04-13 08:30:02

标签: android bitmap

我从某些设备收到此错误。在我的N5上一切都很好。但有些设备,如PRESTIGIO PMT3177 3G引发了这种例外。

setPic代码:

// Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    if(photoW == 0) photoW = 1;
    if(photoH == 0) photoH = 1;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(getCurrentPhotoPath(), bmOptions);

    if (bitmap != null) {

        Bitmap resizedbitmap = null;

        if (targetW > targetH){
            int start = (targetW - targetH) / 2;
            resizedbitmap = Bitmap.createBitmap(bitmap, start, 0, targetH, targetH);
        } else {
            int start = (targetH- targetW) / 2;
            resizedbitmap = Bitmap.createBitmap(bitmap, 0, start, targetW, targetW); //here is exception
        }

logcat的:

 E/image_width﹕ 360
 E/image_height﹕ 360
 E/gridview_width﹕ 1080
 E/gridview_height﹕ 1557
我检查了其他同样的问题: Exception java.lang.IllegalArgumentException: x + width must be <= bitmap.width()

Runtime error "x + width must be <= bitmap.width()" on some devices

IllegalArgumentException: x + width must be <= bitmap.width() in android

createBitmap --- java.lang.IllegalArgumentException: x must be < bitmap.width()

但它继续抛出异常。请解释一下,如何解决这个问题?

0 个答案:

没有答案