0

我正在通过我的设备在我的应用程序中拍照并将其保存到服务器上

我用的是三星note 2

但我收到了这个错误

10-31 20:34:06.759: E/AndroidRuntime(12985): FATAL EXCEPTION: Thread-5431
10-31 20:34:06.759: E/AndroidRuntime(12985): java.lang.OutOfMemoryError
10-31 20:34:06.759: E/AndroidRuntime(12985):    at android.graphics.Bitmap.nativeCreate(Native Method)
10-31 20:34:06.759: E/AndroidRuntime(12985):    at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
10-31 20:34:06.759: E/AndroidRuntime(12985):    at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
10-31 20:34:06.759: E/AndroidRuntime(12985):    at com.winit.dropbox.MainScreen.flip(MainScreen.java:1241)
10-31 20:34:06.759: E/AndroidRuntime(12985):    at com.winit.dropbox.MainScreen$DropBoxUploader.run(MainScreen.java:1166)
10-31 20:34:06.759: E/AndroidRuntime(12985):    at java.lang.Thread.run(Thread.java:856)

代码指向这一行,

        Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);

而 m 是

Matrix m = new Matrix();

编辑:我现在在创建位图期间删除了矩阵,但现在我在调整通过 android 设备拍摄的图像大小时遇到​​了问题,我正在使用

bmp = BitmapsUtiles.getResizedBmp(bmp, AppConstants.DEVICE_WIDTH, AppConstants.DEVICE_HEIGHT);

但它仍然无法正常工作,你能指出我在调整大小时做错了什么吗???

4

1 回答 1

0

这就是我解码位图的方式。希望能帮助到你。基本上我没有加载整个图片,而只是加载所需大小的位图。否则我经常会遇到内存不足的错误。

public static Bitmap decodeSampledBitmapFromResource(String filePath,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    //this avoids memory allocation
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}


private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
于 2013-10-31T15:31:35.943 回答