【问题标题】:NullPointerException rescaling bitmap from file AndroidNullPointerException 从文件 Android 重新缩放位图
【发布时间】:2014-05-14 11:57:48
【问题描述】:

自一个月以来我一直面临这个问题,但没有解决方案: 我需要将文件转换为位图并重新缩放它。 我正在使用以下方法,取自 android 指南:Loading Large Bitmaps Efficiently

public Bitmap decodeSampledBitmapFromFile(String path, int reqSize) {

    // First decode with inJustDecodeBounds=true to check dimensions
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inMutable = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize               
    double ratio = (double) options.outHeight / options.outWidth;
    boolean portrait = true;
    if (ratio < 1) {
        portrait = false;
    }

    double floatSampleSize = options.outHeight / (double)reqSize;
    options.inSampleSize = (int) Math.floor(floatSampleSize);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;     
    Bitmap resultBitmap = BitmapFactory.decodeFile(path, options);

    if(floatSampleSize % options.inSampleSize != 0){
        if(portrait) {
            resultBitmap = Bitmap.createScaledBitmap(resultBitmap, (int)(reqSize/ratio), reqSize, true);                
        } else {
            resultBitmap = Bitmap.createScaledBitmap(resultBitmap, reqSize, (int)(ratio*reqSize), true);                
        }
    }

    return resultBitmap;
}

我有同样的 NullPointerException 但我无法重现它:

NullPointerException (@Utilities:decodeSampledBitmapFromFile:397) {AsyncTask #2}
NullPointerException (@Utilities:decodeSampledBitmapFromFile:399) {AsyncTask #3}

第 397 和 399 行是:

resultBitmap = Bitmap.createScaledBitmap(resultBitmap, (int)(reqSize/ratio), reqSize, true);
resultBitmap = Bitmap.createScaledBitmap(resultBitmap, reqSize, (int)(ratio*reqSize), true);

在此设备型号中:

ST21a
U9500
M7
U8950-1

GT-I9305

有人可以帮我解决这个问题吗? BitmapFactory.decodeFile 是否返回 null? 非常感谢

【问题讨论】:

  • 大多数时候给定的路径可能是错误的。或者,如果您正在解码一个目录中的多个图像,则可能有一个您正试图将其解码为图像的文件夹。确保您没有尝试将文件夹解码为图像并且图像路径有效。使用日志语句查看您要解码的内容。
  • 谢谢 Torcellite,但我只是在调用 decodeSampledBitmapFromFile 之前检查字符串路径是有效的图像文件。无论如何,我会追踪一个日志。

标签: android bitmap nullpointerexception out-of-memory bitmapfactory


【解决方案1】:

试试这个希望对你有用

public Bitmap decodeFile(String path, int reqSize) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = reqSize;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    相关资源
    最近更新 更多