Camera.PictureCallback拍照后停止预览 [英] Camera.PictureCallback stop preview after take a picture

查看:100
本文介绍了Camera.PictureCallback拍照后停止预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用具有Android API 16的设备拍照后,预览将停止工作,但是当我使用具有Android API 22的设备时,该应用程序可以正常工作。

After take a picture using a device with Android API 16, the preview stops working, but when i use a device with Android API 22 the app works fine.

我有这段代码可以拍照。

I have this code for take a picture.

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {

    if (fotofile == null){
        Log.d(TAG, "Erro ao criar arquivo de mídia, verificar permissões de usuário ");
        return;
    }

    try {
        FileOutputStream fos = new FileOutputStream(fotofile);
        fos.write(data);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }
}

};

该代码的调用者是:

 btnFoto = (Button) findViewById(R.id.btnFoto);
        btnFoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fotofile  = new File(dir, "foto_" +System.currentTimeMillis()+ ".jpg");

                if (pressed)
                    return;
                pressed = true;

                mCamera.autoFocus(new Camera.AutoFocusCallback() {
                    public void onAutoFocus(boolean success, Camera camera) {
                        mCamera.takePicture(null, null, mPicture);
                        mToast("Foto Capturada!");
                        pressed = false;
                        shootSound();
                    }
                });    
            }
        });

我的清单

<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE"/>

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

我的build.gradle(Module:app)

My build.gradle (Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "centranet.com.br.cameratalao"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
}

发生错误时使用

mCamera.startPreview(); // After take a picture

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: startPreview failed
            at android.hardware.Camera.startPreview(Native Method)
            at centranet.com.br.cameratalao.MainActivity$2$1.onAutoFocus(MainActivity.java:122)
            at android.hardware.Camera$EventHandler.handleMessage(Camera.java:824)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)


推荐答案

我在Nexus手机(Android 5.1版)中遇到了相同的问题
拍照后停止预览,而在Samsung note 3(Android 5.0)中继续显示预览。
我已解决此问题

I was facing same issue in my Nexus phone (android version 5.1) once it take the picture stops the preview whereas in Samsung note 3 (Android Version 5.0) it continue to display the preview. I fixed this

onPictureTaken(byte[] data, Camera camera) {
            mCamera.startPreview();
            mCamera.startFaceDetection();
.
.
.
}

您需要重新开始面部检测。我不知道为什么它在再次开始预览后会停止。

you need to start face detection again. I don't know why it stops after it starts the preview again.

这篇关于Camera.PictureCallback拍照后停止预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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