Camera Preview doesn't work

All your problems with Audio, Video and Images.

Camera Preview doesn't work

Postby android88 » Tue Jan 05, 2010 9:24 pm

Hello everyone. I'm completely new to android, so forgive me if this is a stupid question :)

I'm trying to run the Camera Preview api demo from google in the android 1.5 emulator, but i keep getting error the "application has stopped unexpectedly" error. Logcat says the following:

01-05 21:13:24.550: WARN/ServiceManager(542): Permission failure: android.permission.CAMERA from uid=10035 pid=867
01-05 21:13:24.561: ERROR/CameraService(542): Permission Denial: can't use the camera pid=867, uid=10035
01-05 21:13:24.580: DEBUG/AndroidRuntime(867): Shutting down VM
01-05 21:13:24.590: WARN/dalvikvm(867): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
01-05 21:13:24.590: ERROR/AndroidRuntime(867): Uncaught handler: thread main exiting due to uncaught exception
01-05 21:13:24.610: ERROR/AndroidRuntime(867): java.lang.RuntimeException: Fail to connect to camera service

From what i read online, the permission error should be fixed by adding a line to the manifest, but that doesn't seem to work. I tried adding two different lines:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.CAMERA" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

and
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.CAMERA"></uses-permission>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


But for both i still get the error. Does anyone see my problem?

Below is the main code. I didn't change anything in the layout file, and i only added the permission line to the manifold.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /*
  2.  
  3.  
  4.  
  5. package nl.example.camerapreview;
  6.  
  7.  
  8.  
  9. import android.app.Activity;
  10.  
  11. import android.content.Context;
  12.  
  13. import android.hardware.Camera;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.view.SurfaceHolder;
  18.  
  19. import android.view.SurfaceView;
  20.  
  21. import android.view.Window;
  22.  
  23. import java.io.IOException;
  24.  
  25.  
  26.  
  27. // ----------------------------------------------------------------------
  28.  
  29.  
  30.  
  31. public class CameraPreview extends Activity {    
  32.  
  33.     private Preview mPreview;
  34.  
  35.  
  36.  
  37.     @Override
  38.  
  39.         protected void onCreate(Bundle savedInstanceState) {
  40.  
  41.         super.onCreate(savedInstanceState);
  42.  
  43.         System.out.println("hoera");
  44.  
  45.         // Hide the window title.
  46.  
  47.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  48.  
  49.  
  50.  
  51.         // Create our Preview view and set it as the content of our activity.
  52.  
  53.         try{
  54.  
  55.             mPreview = new Preview(this);
  56.  
  57.             setContentView(mPreview);
  58.  
  59.             System.out.println("hoera");
  60.  
  61.             }
  62.  
  63.         catch(RuntimeException e){
  64.  
  65.                 System.out.println(e.getMessage());
  66.  
  67.         }
  68.  
  69.     }
  70.  
  71.  
  72.  
  73. }
  74.  
  75.  
  76.  
  77. // ----------------------------------------------------------------------
  78.  
  79.  
  80.  
  81. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  82.  
  83.     SurfaceHolder mHolder;
  84.  
  85.     Camera mCamera;
  86.  
  87.  
  88.  
  89.     Preview(Context context) {
  90.  
  91.         super(context);
  92.  
  93.  
  94.  
  95.         // Install a SurfaceHolder.Callback so we get notified when the
  96.  
  97.         // underlying surface is created and destroyed.
  98.  
  99.         mHolder = getHolder();
  100.  
  101.         mHolder.addCallback(this);
  102.  
  103.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  104.  
  105.     }
  106.  
  107.  
  108.  
  109.     public void surfaceCreated(SurfaceHolder holder) {
  110.  
  111.         // The Surface has been created, acquire the camera and tell it where
  112.  
  113.         // to draw.
  114.  
  115.         mCamera = Camera.open();
  116.  
  117.         try {
  118.  
  119.            mCamera.setPreviewDisplay(holder);
  120.  
  121.         } catch (IOException exception) {
  122.  
  123.             mCamera.release();
  124.  
  125.             mCamera = null;
  126.  
  127.             // TODO: add more exception handling logic here
  128.  
  129.         }
  130.  
  131.     }
  132.  
  133.  
  134.  
  135.     public void surfaceDestroyed(SurfaceHolder holder) {
  136.  
  137.         // Surface will be destroyed when we return, so stop the preview.
  138.  
  139.         // Because the CameraDevice object is not a shared resource, it's very
  140.  
  141.         // important to release it when the activity is paused.
  142.  
  143.         mCamera.stopPreview();
  144.  
  145.         mCamera.release();
  146.  
  147.         mCamera = null;
  148.  
  149.     }
  150.  
  151.  
  152.  
  153.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  154.  
  155.         // Now that the size is known, set up the camera parameters and begin
  156.  
  157.         // the preview.
  158.  
  159.         Camera.Parameters parameters = mCamera.getParameters();
  160.  
  161.         parameters.setPreviewSize(w, h);
  162.  
  163.         mCamera.setParameters(parameters);
  164.  
  165.         mCamera.startPreview();
  166.  
  167.     }
  168.  
  169.  
  170.  
  171. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
android88
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Jan 05, 2010 9:06 pm

Top

Postby Draffodx » Mon Jan 11, 2010 2:55 pm

make sure your permission:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.CAMERA" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


is outside the main application tag in the manifest.

like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.      package="cicero.org.GetCallLog"
  4.  
  5.      android:versionCode="1"
  6.  
  7.      android:versionName="1.0">
  8.  
  9.     <application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar" android:debuggable="true">
  10.  
  11.         <activity android:name=".GetCallLog"
  12.  
  13.                  android:label="@string/app_name">
  14.  
  15.             <intent-filter>
  16.  
  17.                 <action android:name="android.intent.action.MAIN" />
  18.  
  19.                 <category android:name="android.intent.category.LAUNCHER" />
  20.  
  21.             </intent-filter>
  22.  
  23.         </activity>
  24.  
  25.         <provider android:name=".CiceroContentProvider"
  26.  
  27.                                                 android:authorities="cicero.org.GetCallLog.CiceroContentProvider"
  28.  
  29.                                                 android:multiprocess="true" />
  30.  
  31.     </application>
  32.  
  33.    
  34.  
  35.     <uses-sdk android:minSdkVersion="3" />
  36.  
  37.     <uses-permission android:name="android.permission.READ_CONTACTS"/>
  38.  
  39.         <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
  40.  
  41.         <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  42.  
  43.         <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
  44.  
  45.  
  46.  
  47. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 9 guests