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

  • SEARCH
  • Home
  • 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 7993815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:57:53+00:00 2026-06-04T13:57:53+00:00

I tried to implement Camera access like it is posted in several pages. Here’s

  • 0

I tried to implement Camera access like it is posted in several pages.

Here’s the code

 package view;
 import R;
 import android.app.Activity;
 import android.hardware.Camera;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.FrameLayout;
 public class doInspectionImage extends Activity{
private cameraPreview csv;
private Camera cam;
private String rid;
public void onCreate(Bundle is){
    super.onCreate(is);
    this.rid = getIntent().getStringExtra("reportid");
    setContentView(R.layout.spectware_view_takepicture);
    csv = new cameraPreview(this);
    FrameLayout preview = (FrameLayout)findViewById(R.id.picturepreview);
    preview.addView(csv);
    Button takeAPicture = (Button)findViewById(R.id.takePicture);
    takeAPicture.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            cam = csv.getCamera();
            addText();
        }
    });
}
public void onPause(){
    super.onPause();
    csv.CameraDisable();
}
public void onResume(){
    super.onResume();
    csv = new cameraPreview(this);
    FrameLayout preview = (FrameLayout)findViewById(R.id.picturepreview);
    preview.addView(csv);
    Button takeAPicture = (Button)findViewById(R.id.takePicture);
    takeAPicture.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            cam = csv.getCamera();
            addText();
        }
    });
}
private void addText(){
    setContentView(R.layout.spectware_view_picturetext);
    Button storePic = (Button)findViewById(R.id.storepicture);
    storePic.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            EditText dt = (EditText)findViewById(R.id.picturetext);
            storePicture(dt.getText().toString());
            finish();
        }
    });
}
private void storePicture(String txt){
    cam.takePicture(null,null,new model.camera.Camera(rid,txt));
}
}

package view;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder holder;
private Camera camera;
public cameraPreview(Context context){
    super(context);
    this.holder = this.getHolder();
    this.holder.addCallback(this);
    this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder){
        try{
            //Open the Camera in preview mode
            this.camera = Camera.open();
            this.camera.setPreviewDisplay(this.holder);
        }
        catch(IOException ioe)
        {
                ioe.printStackTrace(System.out);
        }
}
public void CameraEnable(SurfaceHolder holder){
    try{
        //Open the Camera in preview mode
        this.camera = Camera.open();
        this.camera.setPreviewDisplay(this.holder);
    }
    catch(IOException ioe)
    {
            ioe.printStackTrace(System.out);
    }
}
public void CameraDisable(){
    this.camera.release();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(width, height);
        camera.setParameters(parameters);
        camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder){
        // Surface will be destroyed when replaced with a new screen
        //Always make sure to release the Camera instance
        camera.stopPreview();
        camera.release();
        camera = null;
}
public Camera getCamera(){
        return this.camera;
}
public void onPause(){

}
}

As soon as I start this activity with

    Intent i = new Intent();
i.setClass(l,doInspectionImage.class);
i.putExtra("reportid",l.inspection_id);
startActivity(i);

I get this message in the logger:

    05-26 02:35:12.550: W/dalvikvm(8300): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
    05-26 02:35:12.600: E/AndroidRuntime(8300): FATAL EXCEPTION: main
    05-26 02:35:12.600: E/AndroidRuntime(8300): java.lang.RuntimeException: Fail to connect to camera service
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.hardware.Camera.native_setup(Native Method)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.hardware.Camera.<init>(Camera.java:300)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.hardware.Camera.open(Camera.java:275)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at view.cameraPreview.surfaceCreated(cameraPreview.java:22)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.SurfaceView.updateWindow(SurfaceView.java:569)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.SurfaceView.updateWindow(SurfaceView.java:407)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:352)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.View.draw(View.java:6936)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.View.draw(View.java:6936)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1917)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewRoot.draw(ViewRoot.java:1530)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1266)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.os.Looper.loop(Looper.java:130)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at android.app.ActivityThread.main(ActivityThread.java:3691)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at java.lang.reflect.Method.invoke(Method.java:507)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    05-26 02:35:12.600: E/AndroidRuntime(8300):     at dalvik.system.NativeStart.main(Native Method)

i stiked to the sample as close as possible.
I don’t know what I did different, besides the changes I had to make to furfil it’s purpose.
I am thankful for any help.

Chris

P.S.: If I click on the back button, I get the preview.
But a second after that the app crashes.

  • 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-04T13:57:55+00:00Added an answer on June 4, 2026 at 1:57 pm

    Two things to check

    1. Check camera permission in manifest. Make sure its added.

    2. You have to properly release the camera resource (stopreview in onPause).

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

Sidebar

Related Questions

I have tried starting the camera in android using an intent, something like this:
I've tried to implement the Google Map View tutorial on the Android developer site,
I tried to implement a simple yes/no input with java.util.Scanner. My code looks like
Tried to implement a reader-writer problem using Blocking Queue with the code given here
I want to do something like this: I tried to implement it by myself
I've searched the forum, and tried to implement the code in the threads I
I tried to implement this. My original post is here iphone: playing audio playlist
i tried to implement this code that move UIImageView's objects, i get a compiler
I have tried to implement this jQuery active menu code: http://docs.jquery.com/Tutorials:Auto-Selecting_Navigation $(function(){ var path
I have tried to implement a onHoverListener on a button in android programming, however,

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.