There is the following photographing code:

Parameters mParameters = mCamera.getParameters(); mParameters.setPictureSize(480, 360); mCamera.setParameters(mParameters); mCamera.setPreviewTexture(new SurfaceTexture(0)); mCamera.startPreview(); Thread.sleep(3000L); final ByteArrayOutputStream i1 = new ByteArrayOutputStream(); mCamera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] i2, Camera i3) { try { i1.write(i2); i1.flush(); i1.close(); } catch (Exception e1) { } } }); 

And there is a code that draws on the resulting photo:

 Bitmap mBitmap = Bitmap.createBitmap(480, 360, Config.ARGB_8888); Canvas mCanvas = new Canvas(mBitmap); Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mCanvas.drawBitmap(BitmapFactory.decodeByteArray(i1.toByteArray(), 0, i1.toByteArray().length), 0, 0, mPaint); mPaint.setColor(Color.argb(128, 0, 0, 0)); mCanvas.drawRect(0, 0, 290, 34, mPaint); mPaint.setColor(Color.WHITE); mPaint.setTextSize(24); String description = "2016.21.10 17:25:30" mCanvas.drawText(description, 8, 26, mPaint); ... mBitmap.compress(CompressFormat.JPEG, 100, mOutputStream); mOutputStream.flush(); mBitmap.recycle(); 

But java.lang.OutOfMemoryError takes off:

java.lang.OutOfMemoryError

at android.graphics.BitmapFactory.nativeDecodeByteArray (Native Method)

at android.graphics.BitmapFactory.decodeByteArray (BitmapFactory.java:443)

at android.graphics.BitmapFactory.decodeByteArray (BitmapFactory.java:460)

at mypackage.MainActivity $ 1 $ 1.run (MainActivity.java:134)

at java.lang.Thread.run (Thread.java:856)

which I got using the try {...} catch (Error e1) {...} . The 134th line contains this:

 mCanvas.drawBitmap(BitmapFactory.decodeByteArray(i1.toByteArray(), 0, i1.toByteArray().length), 0, 0, mPaint); 

How to fix the error? Maybe I have something wrong with the code? The fact is that I tried to do it on several phones, and on one of them the application crashed right away while trying to draw an inscription, and on the other two it crashed only on the third photo. I also tried to do a little differently:

 Bitmap mBitmap = BitmapFactory.decodeByteArray(i1.toByteArray(), 0, i1.toByteArray().length).copy(Config.ARGB_8888, true); ... 

that is, copy the resulting Bitmap , which will be mutable = true . And I also tried to remove the Paint.ANTI_ALIAS_FLAG flag. None of this helped, the error is still the same.

  • your memory is over - Senior Pomidor
  • mCanvas.drawBitmap is asking for too much memory. it lacks and the application crashes. i1 doesn't take too much memory? - Senior Pomidor
  • In the sense of ByteArrayOutputStream ? I doubt it. Now I will try to replace it somehow with byte[] . - nick
  • @SeniorAutomator, no. Just checked. I know a lot of memory is required. But do you really need more than 1 GB? - nick
  • somewhere exactly flows, and most likely there - Senior Pomidor

0