Android take more picture from camera “Couldn't allocate byte array for JPEG data”

不想你离开。 提交于 2019-12-13 05:25:46

问题


I'm trying to take picture in sequence from Android, like a timelapse.

I call

 camera.takePicture(null, null, jpegCallback);

and my jpegCallback is:

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        time++;
        photoCounter.setText(String.valueOf(time));

        SaveImageTask sav = new SaveImageTask(fileNameIND++);
        sav.execute(data);
        resetCam();
        Log.d(TAG, "onPictureTaken - jpeg");
    }
};

private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
    int name;

  SaveImageTask(int name){
      this.name = name;
  }

    @Override
    protected Void doInBackground(byte[]... data) {
        FileOutputStream outStream = null;

        // Write to SD Card
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File (sdCard.getAbsolutePath() + "/TimeLaps");
            dir.mkdirs();



            java.text.NumberFormat nf = new java.text.DecimalFormat("000000");



            String fileName = (nf.format(name)+".jpg");
            File outFile = new File(dir, fileName);


            outStream = new FileOutputStream(outFile);

            outStream.write(data[0]);


            outStream.flush();
            outStream.close();

            Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());

            refreshGallery(outFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);


    }
}

But after taking about 20-30 picture I get:

E/Camera-JNI ] Couldn't allocate byte array for JPEG data

and data from PictureCallBack is null.

Why it happens?? IS the memory full?? How can I free memory??

Thank you so much

来源:https://stackoverflow.com/questions/30645668/android-take-more-picture-from-camera-couldnt-allocate-byte-array-for-jpeg-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!