私は、写真を撮ってファイルに書き込んでから、その写真を base64 配列に読み込む (すべてワンクリックで) という重要な機能の 1 つを備えたアプリケーションを作成しています。問題は、onclick を開始して写真を撮ると、onPhotoTaken() 関数が画像を受け取り、指定されたストレージ ディレクトリに書き込む前に、この関数から返されることです。
コードのいくつかの段階でログ出力を追加しましたが、呼び出した onPhotoTaken() 関数が終了する前に、onclick takePhoto 関数が終了していることは明らかです。
Android のドキュメントには、プレビューを再開する前に JpegCallback が戻るのを待つ必要があると記載されていますが、書き込みが完了するまで待つのに苦労しています。
コード:
public static void takePhoto(){
fCamera.takePicture(null, null, jpegCallback);
Log.d("capture", "photo was captured");
// Set the image callback
Log.d("this one is", "being called");
}
static PictureCallback jpegCallback = new Camera.PictureCallback() {
// Function for handling the picture
public void onPictureTaken(byte[] data, Camera fCamera){
//fCamera.stopPreview();
Log.d("is this", "not being called ??? probably");
File imagePath;
FileOutputStream out = null;
// create the filename with extension
String fileName = "IMAGE_1.bmp";
// Create / Find the storage Directory for our pictures
//storageDir = context.getDir("imageDir", Context.MODE_PRIVATE);
// Create it if it doesn't exist
// Create the image file
imagePath = new File(storageDir, fileName);
String finalPath = imagePath.getAbsolutePath();
Log.d("location", finalPath);
if (!imagePath.exists()) {
if (!imagePath.mkdirs())
Log.d("@string/app_name", "Failed to create File");
return;
}
try {
out = new FileOutputStream(imagePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//finalImage.compress(CompressFormat.PNG, 100, out);
try {
out.write(data);
Log.d("write", "photo was written");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
丸太の猫:
10-13 12:55:39.185: D/capture(7126): photo was captured
10-13 12:55:39.185: D/this one is(7126): being called
これらは、発生する唯一のログ出力です。