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 6183257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:20:02+00:00 2026-05-24T01:20:02+00:00

I have tried starting the camera in android using an intent, something like this:

  • 0

I have tried starting the camera in android using an intent, something like this:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE) 

but since my saved pictures were orientated horizontally and could find a way to fix that I tried building my own camera in android using this marvelous piece of code:

public class EditPhoto extends Activity implements SurfaceHolder.Callback,
        OnClickListener {
    static final int FOTO_MODE = 0;
    private static final String TAG = "CameraTest";
    Camera mCamera;
    boolean mPreviewRunning = false;
    private Context mContext = this;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Log.e(TAG, "onCreate");

        Bundle extras = getIntent().getExtras();

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.editphoto);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceView.setOnClickListener(this);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback(){
        public void onPictureTaken(byte[] imageData, Camera c) {

            if (imageData != null) {

                Intent mIntent = new Intent();

                StoreByteImage(mContext, imageData, 50,
                        "ImageName");
                mCamera.startPreview();

                setResult(FOTO_MODE, mIntent);
                finish();

            }
        }
    };

    protected void onResume() {
        Log.e(TAG, "onResume");
        super.onResume();
    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    protected void onStop() {
        Log.e(TAG, "onStop");
        super.onStop();
    }

    public void surfaceCreated(SurfaceHolder holder) {
        Log.e(TAG, "surfaceCreated");
        mCamera = Camera.open();

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.e(TAG, "surfaceChanged");

        // XXX stopPreview() will crash if preview is not running
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        p.setPreviewSize(w, h);
        mCamera.setParameters(p);
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e(TAG, "surfaceDestroyed");
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;

    public void onClick(View arg0) {

        mCamera.takePicture(null, mPictureCallback, mPictureCallback);

    }

    public static boolean StoreByteImage(Context mContext, byte[] imageData,
            int quality, String expName) {

        File sdImageMainDirectory = new File("/sdcard");
        FileOutputStream fileOutputStream = null;
        String nameFile;
        try {

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;
            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length,options);
            fileOutputStream = new FileOutputStream(
                    sdImageMainDirectory.toString() +"/image.jpg");
            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();

            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

            return true;
             }

}

I have permissions for camera in the manifest file but the only thing that I see is a white screen and a logcat that looks like this:

java.lang.RuntimeException: Fail to connect to camera service
 at android.hardware.Camera.native_setup(Native Method)
 at android.hardware.Camera.<init>(Camera.java:258)
 at android.hardware.Camera.open(Camera.java:235)
  at com.SplashScreen.EditPhoto.surfaceCreated(EditPhoto.java:325)
at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
 at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6883)
 at android.widget.FrameLayout.draw(FrameLayout.java:357)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
at android.view.ViewRoot.draw(ViewRoot.java:1522)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Force finishing activity com.SplashScreen/.EditPhoto

So would you please be kind and tell me what I’m doing wrong?Thank you!

EDIT:Manifest file-

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

XML file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical">

<SurfaceView
 android:id="@+id/surface_camera" 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   />


</LinearLayout>
  • 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-05-24T01:20:03+00:00Added an answer on May 24, 2026 at 1:20 am

    Tour activity EditPhoto.java working perfectly in my API level 7 AVD,

    package x.y;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.graphics.BitmapFactory;
    import android.hardware.Camera;
    import android.graphics.PixelFormat;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.WindowManager;
    
    public class EditPhoto extends Activity implements SurfaceHolder.Callback, OnClickListener {
        static final int FOTO_MODE = 0;
        private static final String TAG = "CameraTest";
        Camera mCamera;
        boolean mPreviewRunning = false;
        private Context mContext = this;
    
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
    
            Log.e(TAG, "onCreate");
    
            Bundle extras = getIntent().getExtras();
    
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.main);
            mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
            mSurfaceView.setOnClickListener(this);
            mSurfaceHolder = mSurfaceView.getHolder();
            mSurfaceHolder.addCallback(this);
            mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
        }
    
        Camera.PictureCallback mPictureCallback = new Camera.PictureCallback(){
            public void onPictureTaken(byte[] imageData, Camera c) {
    
                if (imageData != null) {
    
                    Intent mIntent = new Intent();
    
                    StoreByteImage(mContext, imageData, 50, "ImageName");
                    mCamera.startPreview();
    
                    Bundle b = new Bundle();
                    b.putByteArray("imageData", imageData);
                    Intent i = new Intent(mContext, ImageDisplayActivity.class);
                    i.putExtras(b);
                    startActivity(i);
    
                    setResult(FOTO_MODE, mIntent);
                    finish();
    
                }
            }
        };
    
        protected void onResume() {
            Log.e(TAG, "onResume");
            super.onResume();
        }
    
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
        }
    
        protected void onStop() {
            Log.e(TAG, "onStop");
            super.onStop();
        }
    
        public void surfaceCreated(SurfaceHolder holder) {
            Log.e(TAG, "surfaceCreated");
            mCamera = Camera.open();
    
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            Log.e(TAG, "surfaceChanged");
    
            // XXX stopPreview() will crash if preview is not running
            if (mPreviewRunning) {
                mCamera.stopPreview();
            }
    
            Camera.Parameters p = mCamera.getParameters();
            p.setPreviewSize(w, h);
            mCamera.setParameters(p);
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.e(TAG, "surfaceDestroyed");
            mCamera.stopPreview();
            mPreviewRunning = false;
            mCamera.release();
        }
    
        private SurfaceView mSurfaceView;
        private SurfaceHolder mSurfaceHolder;
    
        public void onClick(View arg0) {
    
            mCamera.takePicture(null, mPictureCallback, mPictureCallback);
    
        }
    
        public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {
    
            File sdImageMainDirectory = new File("/sdcard");
            FileOutputStream fileOutputStream = null;
            String nameFile;
            try {
    
                BitmapFactory.Options options=new BitmapFactory.Options();
                options.inSampleSize = 5;
                Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,options);
                fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() +"/image.jpg");
                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
    
                myImage.compress(CompressFormat.JPEG, quality, bos);
    
                bos.flush();
                bos.close();
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return true;
        }
    
    }
    

    i am posting my Layout and Manifest here …

    Layout editphoto.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <SurfaceView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
       android:id="@+id/surface_camera"
        />
    </LinearLayout>
    

    and Manifest file …

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="x.y"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".EditPhoto"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        <activity android:name="ImageDisplayActivity"></activity>
    </application>
    
    
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    
    </manifest> 
    

    New Activity to display image ImageDisplayActivity.java (Image will be displayed when you will click on surface view)….

    package x.y;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class ImageDisplayActivity extends Activity {
    
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            Bundle extras = getIntent().getExtras();
            setContentView(R.layout.anotherlayout);
    
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;
            byte[] imageData = extras.getByteArray("imageData");
            Bitmap myImage = BitmapFactory.decodeByteArray(imageData , 0, imageData.length,options);
    
    
            ImageView imageView = (ImageView) findViewById(R.id.myPic);
            imageView.setImageBitmap(myImage);
        }   
    }
    

    and it’s layout otherlayout.xml …

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_gravity="center"
       android:id="@+id/myPic"
        />
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is really starting to piss me off. I feel like I have tried
I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have tried to find how to create DLL-s on linux using google, but
Starting from this example: http://support.microsoft.com/kb/828736 I have tried to add a test function in
I have tried starting the process with 1,2,3,4 GB of memory but still get
Update : I have tried using fuslogvw.exe to get logs. But what exactly am
I have tried to figure this out on my own... starting with two excellent
I've tried starting view based app and window based app but I still have
Starting a new thread... about same question.. I have tried all I am getting
I know this question has been asked before, but I have tried the given

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.