1

I have a service class which is taking picture and writing it to sdcard using camera. Now I want that I should call that service every 10 seconds so that I can take image from the camera every 10 seconds. however am getting exception. Here is my code for the main activity:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(com.example.picturesenderbluetooth.R.layout.capture_and_send);

    Button cap = (Button)findViewById(R.id.button1);
    cap.setOnClickListener(new OnClickListener() {

        @Override 
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new Thread(new Task()).start();
        }
    });

    }

public void turnbluetoothon()
{
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
    if (mBluetoothAdapter.disable()) {
        mBluetoothAdapter.enable(); 
    } 
}


class Task implements Runnable { 

    @Override 
    public void run() { 
        while(true) { 
            try { 
                Log.w("Rami code yaar","Rami mei phir chal gaya... hahaha");
                startService(new Intent(getBaseContext(), Backgroundservice.class));
                Thread.sleep(10000); 
                stopService(new Intent(getBaseContext(), Backgroundservice.class));

            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
        } 
  } 

 }
}

The logcat shows:

12-20 17:11:25.854: E/AndroidRuntime(9859): FATAL EXCEPTION: main
12-20 17:11:25.854: E/AndroidRuntime(9859): java.lang.RuntimeException: Unable to start service com.example.Picture_Sender_Bluetooth.Backgroundservice@419b5920 with Intent { cmp=com.example.picturesenderbluetooth/com.example.Picture_Sender_Bluetooth.Backgroundservice }: java.lang.RuntimeException: Fail to connect to camera service
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2829)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread.access$1900(ActivityThread.java:156)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.os.Looper.loop(Looper.java:153)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread.main(ActivityThread.java:5336)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at java.lang.reflect.Method.invoke(Method.java:511)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at dalvik.system.NativeStart.main(Native Method)
12-20 17:11:25.854: E/AndroidRuntime(9859): Caused by: java.lang.RuntimeException: Fail to connect to camera service
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.hardware.Camera.native_setup(Native Method)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.hardware.Camera.<init>(Camera.java:445)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.hardware.Camera.open(Camera.java:421)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at com.example.Picture_Sender_Bluetooth.Backgroundservice.onStart(Backgroundservice.java:37)

whats making the application crash? how will I fix it??

ali
  • 11
  • 4
  • Have you added the permission to the manifest file? – Heskja Dec 20 '14 at 12:19
  • possible duplicate of [android java lang runtimeexception fail to connect to camera service](http://stackoverflow.com/questions/23904459/android-java-lang-runtimeexception-fail-to-connect-to-camera-service) – marcinj Dec 20 '14 at 12:24
  • I did sir I added the permission – ali Dec 20 '14 at 12:24

1 Answers1

0

I found out whats the issue. In the service which I made. I was not releasing my camera. So I called the function.

cameraobject. release();

The code is running fine now!

ali
  • 11
  • 4