1

i have for days now try to get my project to work but it didn't, there are similar questions and answers on this site but non help me solve my problem that is why i am asking this question with complete code below.

I am creating a camera project using the Android camera API, and what i want is just to startPreview on a surface for the mean time, but i kept getting a runtime error.

 04-10 22:52:28.690: D/com.Server.camerapreview.PreviewActivity(1194): SurfaceChanged: startPreview failed

And my activity xml is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PreviewActivity" >

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

My android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Server.camerapreview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.Server.camerapreview.PreviewActivity"
            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>

</manifest>

and my main activity is:

  package com.Server.camerapreview;

//import android.graphics.PixelFormat;

import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.app.Activity;


public class PreviewActivity extends Activity implements SurfaceHolder.Callback{

    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    Camera camera = null;
    boolean inPreview = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);


    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        if(camera != null){
            try{
                camera.setPreviewDisplay(surfaceHolder);
            }catch(Exception x){
                Log.d(PreviewActivity.class.getName(), "Error in surface created: ["+x.getMessage()+"]");
            }
        }else Log.d(PreviewActivity.class.getName(), "Camera null");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (surfaceHolder.getSurface() == null){
             Log.d(PreviewActivity.class.getName(), "SurfaceHolder is null");
              return;
            }


        if(camera != null && !inPreview){    
            Camera.Parameters parameters = camera.getParameters();
            Camera.Size size = getBestPreviewSize(width, height, parameters);

            if (size != null) {

                parameters.setPreviewSize(size.width, size.height);
                //parameters.setPictureFormat();
               // Log.d(PreviewActivity.class.getName(), "size: "+surfaceHolder;
                parameters.set("orientation", "portrait");
                camera.setParameters(parameters);
                try {
                    camera.setPreviewDisplay(surfaceHolder);
                    surfaceView.setVisibility(0);
                    camera.startPreview();

                    inPreview = true;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Log.d(PreviewActivity.class.getName(), "SurfaceChanged: "+e.getMessage());
                }
            }

        }else Log.d(PreviewActivity.class.getName(), "Camera null");

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if(inPreview){
            camera.stopPreview();
            camera.release();
            camera = null;
            inPreview = false;
        }
    }

    // Getting the best preview size
    private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
        Camera.Size result = null;

        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            if (size.width <= width && size.height <= height) {
                if (result == null) {
                  result=size;
                }
                else {
                    int resultArea = result.width * result.height;
                    int newArea = size.width * size.height;

                    if (newArea > resultArea) {
                        result = size;
                    }
                }
            }
        }

        return(result);
    }


}
n1x0rd0n
  • 21
  • 6
  • Have you tried changing the IOException to an Exception? I'm guessing your preview display isn't valid (for some reason) and catching the Exception might give you a bit more detail – DigCamara Apr 10 '13 at 16:12
  • @DigCamara changed it to Exception (thanks). I didn't get all those errors the only thing i got was my code from the catch block: 04-10 18:42:58.050: D/com.Server.camerapreview.PreviewActivity(30721): SurfaceChanged: startPreview failed And then the screen stayed black, nothing was hapening. – n1x0rd0n Apr 10 '13 at 16:45
  • Yeah, it wasn't meant to actually correct your error, but I thought maybe you were missing out on some detail. My guess your surfaceHolder is somehow wrong but I haven't used those objects so I don't have any more information. Can you debug it and check if the object is valid at that point? – DigCamara Apr 10 '13 at 16:48
  • I tried alot of things, still when i start the app on my phone it opens and the screen stays blank it doesn't crash, but there is a startPreview failed message caught by try/catch block in my surfaceChanged method. – n1x0rd0n Apr 10 '13 at 21:12

1 Answers1

1

This is not actually a solution but i solved the problem by adding

surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

and my application works, but there is a cross on it indicating that it has been deprecated because i am using API level 17 for my builds, does anyone know an alternate method that is not deprecated and can be used within this API level?

n1x0rd0n
  • 21
  • 6
  • As mentioned in this question the call is required prior Honeycomb. http://stackoverflow.com/questions/9439186/surfaceholder-settype-is-deprecated-but-required – multiholle Feb 08 '14 at 23:18