2013-10-31 52 views
0

我拍照通过我的设备我的应用程序,并使用三星注2如何在android中处理java.lang.OutOfMemoryError错误?

保存该到服务器

IM,但我发现这个错误

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); 

IM但仍不能正常工作,你能指出什么我做错了,而调整大小?

+1

请看看开发者网站:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html –

+0

你怎么知道那是只有2MB?宽度*高度* 4 = 2MB? – Selvin

+0

我已经检查了以前文章中已经提到的所有链接 – Ari

回答

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; 
} 
+0

我正面临问题,同时调整通过Android设备拍摄的图像,即时通讯使用 bmp = BitmapsUtiles.getResizedBmp(bmp,AppConstants.DEVICE_WIDTH,AppConstants.DEVICE_HEIGHT); 但它仍然不工作,你可以请指出我调整大小时,我做错了什么? – Ari

+0

老实说,我不知道,我从来没有使用过这种方法。我最好的猜测是图像在解码后被调整大小。试试我写给你的这个方法:decodeSampledBitmapFromResource。 –