bitmap options.outWidthは-1

3017 ワード

解決方法:
 public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight, String filePath) {
        int height = options.outHeight;
        int width = options.outWidth;
        int inSampleSize = 1;
        if (options.outHeight == -1 || options.outWidth == -1) {
            try {
                ExifInterface exifInterface = new ExifInterface(filePath);
                int tempHeight = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH
                        , ExifInterface.ORIENTATION_NORMAL);
                int tempWidth = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH
                        , ExifInterface.ORIENTATION_NORMAL);

                width = tempHeight;
                height = tempWidth;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

 public static Bitmap getSmallBitmap(String filePath) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = calculateInSampleSize(options, 480, 800, filePath);
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }