0

I am trying to build an app which can turn on and off Camera Flash of my device . In Code its not showing any error but while launching the app on my device it's start to Crash . Report bug is showing some Unable to start activity and something like Fail to connect Camera Services . Since I am new in android Development and don't have enough knowledge. I had already seen few questions regarding this but not able to find some useful information. All answers were approximately suggesting adding using permission in manifest file which I already did.

content of manifest file:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.joshiyogesh.flashlight">
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        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>

</manifest>

content of Main-activity Java:

package com.example.joshiyogesh.flashlight;

import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btn;
    android.hardware.Camera camera;
    Camera.Parameters parameters;
    boolean isFlash = false;
    boolean isOn = false;
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.button2);
        if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
        {
            camera = Camera.open();
            parameters = camera.getParameters();
            isFlash = true;
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             if(isFlash){
                 if(!isOn){
                     btn.setText("Off");
                     btn.setBackgroundColor(Color.GREEN);
                     parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                     camera.setParameters(parameters);
                     camera.startPreview();
                     isOn = true;
                 }
                 else{
                     btn.setText("ON");
                     btn.setBackgroundColor(Color.RED);
                     parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                     camera.setParameters(parameters);
                     camera.stopPreview();
                     isOn = false;
                 }
             }

             else{
                 Toast.makeText(MainActivity.this,"Camera Not detecting",Toast.LENGTH_LONG).show();

             }

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(camera!=null){
            camera.release();
            camera = null;
        }
    }
}

I have uploaded image of report bug of my mobile device

Joshi Yogesh
  • 142
  • 1
  • 12

3 Answers3

0

I'm posting the code to open the camera,please make changes as per your need.

try this:

static Camera camera = null;

and declare the following:

 try{ 
 if(clickOn == true) {
   clickOn = false;
   camera = Camera.open();
   Parameters parameters = camera.getParameters();
   parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
   camera.setParameters(parameters);
   camera.startPreview();

   remoteViews.setViewVisibility(R.id.button1, View.GONE);
   remoteViews.setViewVisibility(R.id.button2, View.VISIBLE);
   localAppWidgetManager.updateAppWidget(componentName, remoteViews);
  } else {
   clickOn = true;
   camera.stopPreview();
   camera.release();
   camera = null;

   remoteViews.setViewVisibility(R.id.button1, View.VISIBLE);
   remoteViews.setViewVisibility(R.id.button2, View.GONE);
   localAppWidgetManager.updateAppWidget(componentName, remoteViews);
 }    
} catch(Exception e) {
     Log.e("Error", ""+e);
}

After using camera, Don't forget to release it by following statement:

camera.release();

Also it may be the case that your app is not given permission to open camera at run time. Because from android 6.0 it is necessary to to have runtime permission to do specific tasks.

So if you are using Android 6.0(Marshmallow) or above, please check that the permission is enabled or not by following this:

The permission for camera could be disabled and should be enabled from the app settings. Settings -> Apps -> [Your App] -> Permissions.

That is what worked for me. Hope this help you too :)

EDIT And please use e.printstacktrace() in your catch block to get error logcat.

Kaushal28
  • 503
  • 1
  • 6
  • 18
0

i think there is problem while releasing the camera . Since you have written release method in onStop ,your camera will get release only when app is closed. try to release the camera when you are turning off the flash in button click method.

0

I got where i was wrong . Codes written above are Right . The only problem , i was using marshmallow in which we have to give permission to obtain camera permission through device .

Joshi Yogesh
  • 142
  • 1
  • 12