我正试图从Android中按顺序拍照,就像游戏中时光倒流一样。
我打电话
camera.takePicture(null, null, jpegCallback);
我的jpegCallback是:
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);
}
}
但在拍摄了大约20-30张照片后,我得到了:
E / Camera-JNI] 无法为JPEG数据分配字节数组
并且PictureCallBack中的数据为空。
为什么会这样? 是内存满?我怎样才能释放内存?
非常感谢