Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:45:46+00:00 2026-06-03T23:45:46+00:00

My Application Work Perfect But When My Camera Appliaction When Start And Close Then

  • 0

My Application Work Perfect But When My Camera Appliaction When Start And Close Then Again Start Then.
java.lang.RuntimeException: Fail to connect to camera service
This Error Generate Plz Any Body Help Me.

Thanks For Advance…

Code
`
CameraActivity.java

 package com.drc.camera;

  public class CameraActivity extends Activity { 

     private static final String TAG = "CameraDemo";
      CameraPreview mCameraPreview=null;
      FrameLayout mFrameLayout;
      Button btntakephoto,btnresetphoto; 

      @Override
      public void onCreate(Bundle savedInstanceState)       
      {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.xcamera);
        mFrameLayout=(FrameLayout)findViewById(R.id.framepreview);
            btntakephoto = (Button) findViewById(R.id.btntakephoto);
        btnresetphoto = (Button) findViewById(R.id.btnresetphoto);


        mCameraPreview = new CameraPreview(this); 
        mFrameLayout.addView(mCameraPreview);

        btnresetphoto.setEnabled(false);

        btntakephoto.setOnClickListener(new OnClickListener() {
          public void onClick(View v) { 
            mCameraPreview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            btnresetphoto.setEnabled(true);
          }
        });


        btnresetphoto.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mCameraPreview.camera.startPreview();
                btnresetphoto.setEnabled(false);
                Toast.makeText(CameraActivity.this,"Start Camera", Toast.LENGTH_SHORT).show();
            }
        });

        Log.d(TAG, "onCreate'd");
      }
      ShutterCallback shutterCallback = new ShutterCallback() { 
        public void onShutter() {
          Log.d(TAG, "onShutter'd");
        }
      };

      PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
          Log.d(TAG, "onPictureTaken - raw");
        }
      };

      PictureCallback jpegCallback = new PictureCallback() { 
        public void onPictureTaken(byte[] data, Camera camera) {
          FileOutputStream outStream = null;
          try {
                outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
              }
          catch (FileNotFoundException e) {
                e.printStackTrace();
              }
          catch (IOException e) {
                e.printStackTrace();
              }
          Log.d(TAG, "onPictureTaken - jpeg");
        }
      };

}

`
`

CameraPreview.java

 package com.drc.camera;

    import java.io.IOException;

    class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 
      private static final String TAG = "Preview";

      SurfaceHolder mHolder;  
      public Camera camera; 

      CameraPreview(Context context) {
        super(context);
        mHolder = getHolder();  
        mHolder.addCallback(this);  
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
      }

      public void surfaceCreated(SurfaceHolder holder) {  

        camera = Camera.open(); 
        try {
          camera.setPreviewDisplay(holder); 

          camera.setPreviewCallback(new PreviewCallback() { 
            public void onPreviewFrame(byte[] data, Camera camera) {  
              Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
              CameraPreview.this.invalidate(); 
            }
          });
        }
       catch (IOException e) { 
          e.printStackTrace();
        }
      }

      public void surfaceDestroyed(SurfaceHolder holder) {  
        camera.stopPreview();
        camera = null;
      }

      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            Camera.Parameters parameters = camera.getParameters();
            parameters.setPreviewSize(w, h);
            camera.setParameters(parameters);
            camera.startPreview();
        }

        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
            Paint p = new Paint(Color.RED);
            Log.d(TAG, "draw");
            canvas.drawText("PREVIEW", canvas.getWidth() / 2,canvas.getHeight() / 2, p);
        }

    }

`
AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".CameraActivity"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:screenOrientation="landscape"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"></uses-feature> 

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:45:47+00:00Added an answer on June 3, 2026 at 11:45 pm

    Make sure you call camera.release() in onPause(), not just set it to null. It’s permissible for an application to keep the camera running while not in the foreground (for example, so you can keep using video chat while you check something in a different application).

    This means you have to clean up after yourself once you’re done using the camera, and call release(). Until you do, or at least before GC cleans up or your application process is killed, no other app can access the camera.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm updating code in an old Java application at work but I'm pretty new
I have a duplex WCF, it work perfect in local host (connect my application
I'm trying to get the browser function in my Android application to work but
Every time I open my Terminal application at work it starts from a clean
I am currently developing an application to work with WAV files. I want to
I'm just writing a test application to work out how I can optimise searching
I'm trying to get a CakePHP application to work. For this, I've set up
We are using Ext JS for an application in work, building a custom theme
I have a WCF service and a Silverlight application that work just fine when
I am a student programmer using QT to develop and application for work. Currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.