Alejandro Balegno Alejandro Balegno - 4 months ago 24
Android Question

Why camera not work in Android 5?

In android 2.3, 4, 4.1, 4.2, 4.3, 4.4 works correctly

I've this function with android 2.3 api:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// 1º Si se esta viendo preview, se detiene
if (mPreviewRunning) {
mCamera.stopPreview();
}
// 2º Se setean los nuevos parametros de la camara
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
mCamera.setParameters(p);

// 3º Volvemos a mostrar el preview
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(ctxt, "Error refreshing camera", Toast.LENGTH_LONG)
.show();
}

mCamera.startPreview();
mPreviewRunning = true;
}


The app crash in line, return

error: W/System.err: java.lang.RuntimeException: Fail to connect to camera service

mCamera.setParameters(p);


after return this:

W/System.err: java.io.IOException: open failed: ENOENT (No such file or directory)

This is my manifest for my two apps:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foca.deboSancion"
android:versionCode="4"
android:versionName="14.7" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foca.deboComun"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-feature android:name="android.hardware.camera" />
<uses-sdk android:minSdkVersion="8" />

Answer

Well mCamera.setParameters(p); is not lollypop(android 5.0) api issue, its device specific. Most of the devices behaviour with the camera parameters are is final, I mean ur cannot change it, if you try it will crash. Easy possible solution use of try catch, advanced is use reflection (suggestion : dont go for this)

Comments