使用带有Android API 16的设备拍摄照片后,预览会停止工作,但是当我使用带有Android API 22的设备时,应用程序运行良好。
我有这个代码可以拍照。
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(模块: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)
答案 0 :(得分:4)
我在Nexus手机中面临同样的问题(Android版本5.1) 一旦它拍摄图片停止预览,而在三星笔记3(Android版本5.0)它继续显示预览。 我修好了这个
onPictureTaken(byte[] data, Camera camera) {
mCamera.startPreview();
mCamera.startFaceDetection();
.
.
.
}
你需要再次开始面部检测。我不知道为什么它再次开始预览后会停止。