Android Camera takePicture函数不调用回调函数 [英] Android Camera takePicture function does not call Callback function

查看:325
本文介绍了Android Camera takePicture函数不调用回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序进行自定义Camera活动. 我正在按照Android开发者网站上的说明进行操作: http://developer.android.com/guide/topics/media/camera.html 除了未调用Callback函数和不保存图片之外,其他所有内容似乎都正常运行.这是我的代码:

I am working on a custom Camera activity for my application. I was following the instruction from the Android Developers site here: http://developer.android.com/guide/topics/media/camera.html Everything seems to works fine, except the Callback function is not called and the picture is not saved. Here is my code:

public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private static final String TAG = "CameraActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v(TAG, "will now take picture");
            mCamera.takePicture(null, null, mPicture);
            Log.v(TAG, "will now release camera");
            mCamera.release();
            Log.v(TAG, "will now call finish()");
            finish();
        }
    });        
}

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.v(TAG, "Getting output media file");
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            Log.v(TAG, "Error creating output file");
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.v(TAG, e.getMessage());
        } catch (IOException e) {
            Log.v(TAG, e.getMessage());
        }
    }
};

private static File getOutputMediaFile() {
    String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    else {
        File folder_gui = new File(Environment.getExternalStorageDirectory() + File.separator + "GUI");
        if (!folder_gui.exists()) {
            Log.v(TAG, "Creating folder: " + folder_gui.getAbsolutePath());
            folder_gui.mkdirs();
        }
        File outFile = new File(folder_gui, "temp.jpg");
        Log.v(TAG, "Returnng file: " + outFile.getAbsolutePath());
        return outFile;
    }
}

单击按钮后,我得到以下日志:现在将拍照",现在将释放相机"和现在将结束通话".活动成功完成,但是在

After clicking the Button, I get logs: "will now take picture", "will now release camera" and "will now call finish". The activity finishes succesfully, but the Callback function was not called during the

mCamera.takePicture(null, null, mPicture);

函数(没有来自mPicture回调或getMediaOutputFile函数的日志),并且在指定的位置没有文件.

function (There were no logs from the mPicture callback or getMediaOutputFile functions) and there is no file in the location that was specified.

有什么想法吗? :) 非常感谢!

Any ideas? :) Much thanks!

推荐答案

mCamera.takePicture()的调用是异步的.这意味着呼叫会立即完成,但是实际的拍照和处理将在稍后的某个时间进行.但是,您可以在从调用返回到mCamera.takePicture()时立即执行此操作:

The call to mCamera.takePicture() is asynchronous. That means that the call completes immediately, but the actual picture taking and processing happens sometime later. However, you do this immediately upon return from the call to mCamera.takePicture():

Log.v(TAG, "will now release camera");
mCamera.release();
Log.v(TAG, "will now call finish()");
finish();

这意味着您已经释放了相机资源并完成了活动,然后相机才有机会实际拍摄照片并给您回电.

That means that you have released the camera resources and finished the Activity before the camera gets a chance to actually take the picture and call you back.

您需要将该代码移到回调方法onPictureTaken()中,以便在发生回调后释放资源并完成Activity.

You need to move that code into the callback method onPictureTaken() so that you release the resources and finish the Activity after the callback occurs.

这篇关于Android Camera takePicture函数不调用回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆