2

I'm a begginer android programmer and I have a problem with the camera. What I'm trying to do is to get a preview on the camera. The problem is, Camera.open() always returns null. The code goes like this:

CameraPreview.java

public class CameraPreview extends Activity implements SurfaceHolder.Callback
{
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_camera_preview);

        SurfaceHolder holder;
        SurfaceView preview = (SurfaceView) findViewById(R.id.surface1);
        holder = preview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        Camera camera = Camera.open();
        if(camera!=null)
        {
            try 
            {
                camera.setPreviewDisplay(holder);
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            camera.startPreview();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub      
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.paparazzi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".CameraPreview"> </activity>
</application>

emulator config:

hw.lcd.density=240
hw.cpu.arch=arm
skin.name=WVGA800
sdcard.size=64M
abi.type=armeabi-v7a
hw.camera.back=emulated
image.sysdir.1=system-images\android-16\armeabi-v7a\
hw.gpu.enabled=yes
hw.camera.front=emulated
skin.path=platforms\android-16\skins\WVGA800
hw.cpu.model=cortex-a8
vm.heapSize=48
hw.ramSize=512

I use eclipse with android plugin and the newest sdk. I would really appreciate any suggestions.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • Test on Real Device....Not on emulator as it does not support Camera Resource... – Mainank Brahmbhatt Jul 11 '12 at 11:27
  • Ok, thank you for your advices, but it still doesn't work. I will put it simple this time - Do you think it is even possible to get camera to work properly on emulator, or should I use the real device? – Michał Żętkowski Jul 11 '12 at 11:59
  • You cannot test in on emulator because the camera is not working on emulator. You will have to test your application on a real device as Mainank has already told you – banzai86 Jul 11 '12 at 12:05

3 Answers3

0

You haven't given following feature in the Manifest. Give it and run it again.

 <uses-feature android:name="android.hardware.camera" android:required="true"/>

If still getting the Exception, put your Logcat output here.

Updated:

private static Camera camera;

    public static Camera getCameraInstance(){
        try {
            camera = Camera.open();
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
            Toast.makeText(context, "Camera is occupied by another program" , Toast.LENGTH_SHORT).show();
        }
        return camera; // returns null if camera is unavailable
    }

Use above code in your's and try to extract the exception if it getting again and again.

Call

CameraPreview.getCameraInstance();

inside your surfaceCreated method

Thanks @alextsc to remind me about the uses-feature

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • 2
    This is a) Not required. It's merely a hint for markets (e.g. google play) to filter apps based on some criteria b) Already included implicitly. If you request the camera permission without a ``, this feature is assumed. See [the docs](http://developer.android.com/guide/topics/manifest/uses-feature-element.html#implicit). You can spell it out to make your manifest/code more readable though, but thats about it. –  Jul 11 '12 at 11:29
0

before API version 14, the emulator doesn't support camera simulation. Even after the update the documentation doesn't really talk about it.

there is a workaround to this - check it out here.

http://www.tomgibara.com/android/camera-source

Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

using android’s inbuilt camera app, launching the camera and taking the picture can done with very few lines of code using the power of Intent

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
Maha 3mer
  • 55
  • 1
  • 3