-4

i am developing an android app which has an flash option which is set to auto mode,but it crashes at camera.open.I have used intent to open camera

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            int result = context.checkCallingOrSelfPermission(Manifest.permission.CAMERA);
            int result2 = context.checkCallingOrSelfPermission(Manifest.permission.FLASHLIGHT);
            if((result==PackageManager.PERMISSION_GRANTED) && (result2==PackageManager.PERMISSION_GRANTED)) {
                cam = Camera.open();
                Camera.Parameters p = cam.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                cam.setParameters(p);
                cam.startPreview();
            }
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(cameraIntent,CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
        }});

HERE IS LOGCAT

             04-10 15:44:58.928 13248-13248/com.t4u.aapam E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: com.t4u.aapam, PID: 13248
                                                           java.lang.RuntimeException: Fail to connect to camera service
                                                               at android.hardware.Camera.<init>(Camera.java:529)
                                                               at android.hardware.Camera.open(Camera.java:379)
                                                               at com.t4u.aapam.ListViewDisplay$1.onItemClick(ListViewDisplay.java:402)
                                                               at android.widget.AdapterView.performItemClick(AdapterView.java:305)
                                                               at android.widget.AbsListView.performItemClick(AbsListView.java:1148)
                                                               at android.widget.AbsListView$PerformClick.run(AbsListView.java:3059)
                                                               at android.widget.AbsListView$3.run(AbsListView.java:3866)
                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                               at android.os.Looper.loop(Looper.java:135)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

here is my manifest file.i have added camera permission and flashlight permisssion.I have also added camera hardware permisssion

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.t4u.aapam">

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"       />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.telematics4u.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera"    android:required="false" />
<uses-feature android:name="android.hardware.camera.flash"
    android:required="false" />
<application
    android:name="com.t4u.aapam.App"
    android:allowBackup="true"
    android:icon="@drawable/launcher_logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

shivadeep
  • 68
  • 1
  • 11
  • Are you testing the app on some kind of emulator or real device? – Simranjeet Singh Apr 10 '17 at 10:29
  • There are plenty of similar issues, search `java.lang.RuntimeException: Fail to connect to camera service` in Google and you will get plenty of solutions . Like these ones http://stackoverflow.com/questions/23904459/android-java-lang-runtimeexception-fail-to-connect-to-camera-service , http://stackoverflow.com/questions/26305107/how-to-fix-fail-to-connect-to-camera-service-exception-in-android-emulator – Sunil Sunny Apr 10 '17 at 10:29
  • i have tested it on real device – shivadeep Apr 10 '17 at 10:36

2 Answers2

1

I may be wrong(because I don't see your Manifest.xml file) but there are two solutions I can see

1) The camera cannot be connected to because it is already being used by a different application

You cannot fix that. If the camera is occupied, you can't open it.

2) You haven't requested the Camera-permission.

This can be solved. In your manifest:

<uses-permission android:name="android.permission.CAMERA"/>

And if you are targeting android 6 you have to request the permission at runtime. For that, see this link.

EDIT:

Make sure you add all of these. This will give your app access to the camera and flashlight in software and hardware.

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.flash"/>

Using these without requiring them makes the app not work if the device (against all odds) doesn't have a camera

Community
  • 1
  • 1
Zoe
  • 27,060
  • 21
  • 118
  • 148
0

you should use all these permissions

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" 
 android:required="false" />

Maybe this can help but i am not sure

<permission android:name="android.permission.FLASHLIGHT"
         android:permissionGroup="android.permission-
group.HARDWARE_CONTROLS"
         android:protectionLevel="normal"/>
Anmol317
  • 1,356
  • 1
  • 14
  • 31