2

I am trying to build a barcode-scanning app using Zxing Library on my phone (HTC One). Everything works fine but on older phones (Galaxy S2) I am getting an OutOfMemory Exception while creating the intArray. I am taking the pictures with a resolution of 2MP (1600x1200), when I took a smaller resolution my phone couldn't read barcodes anymore and i still get the exception.

Is there any way to avoid the heap overflow without decreasing the picture size?

Code:

 public String decode(File f) {
    String content;
    Bitmap bMap = BitmapFactory.decodeFile(f.getAbsolutePath());
    int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
    int with = bMap.getWidth();
    int height = bMap.getHeight();
    bMap.getPixels(intArray, 0, with, 0, 0, with, height);
    bMap.recycle();
    bMap = null;
    LuminanceSource source = new RGBLuminanceSource(with, height, intArray);
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
    Reader reader = new MultiFormatReader();
    try {
        Result result = reader.decode(binaryBitmap);
        content = result.getText();
        return content;
    } catch (NotFoundException e) {
        return null;
    } catch (ChecksumException e) {

    } catch (FormatException e) {

    } finally {
        intArray = null;
        binaryBitmap = null;
        System.gc();
    }
    return "No Code found";
}

Exception

07-23 10:09:01.720  26626-26626/com.example.chris.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: startPreview failed
        at android.hardware.Camera.startPreview(Native Method)
        at at.schaefer.ssi.smartpdc.camera.ScanCamera$2.onAutoFocus(ScanCamera.java:80)
        at android.hardware.Camera$EventHandler.handleMessage(Camera.java:858)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)
  • I think you can remove the line `System.gc()`. – Davlog Jul 23 '14 at 08:50
  • How about trying to enlarge heap size by the methond mentioned in http://stackoverflow.com/a/11275719/718379 ? – timrau Jul 23 '14 at 08:55
  • If I'm reading this correctly, your app doesn't crash when using <2MP (if so, please make this clear in your question)? The `dalvik.system.NativeStart.main(Native Method)` makes me think it could be a caching error with Android; clear your phone's cache and try again. – AStopher Jul 23 '14 at 09:02
  • Clearing the Cache changed nothing – user3807204 Jul 23 '14 at 09:33

0 Answers0