Reputation: 53
i just wanna try to Access my Camera on Android. I dont know how i can get error logs, but i get an error when i call the function Camera.open().
Im using the SDK example Code, but it didnt works.
I also tried out to set mCamera to null before releasing. And yes i have setted the permission in the manifest.xml
it didnt works with the emulator even with my htc
package com.example.android.apis.graphics;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import java.io.IOException;
public class CameraPreview extends Activity {
private Preview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
Upvotes: 3
Views: 20932
Reputation: 11
if you already place permissions above <application>
element in AndroidManifest and it not help,
check your import, it should be import android.hardware.Camera
Upvotes: 0
Reputation: 3434
Place permissions above the Application tag, in your manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application>
<activity></activity>
</application>
i kept the permissions on wrong place thats why i kept on getting the same error! Hope this will help you too
Upvotes: 1
Reputation: 33605
In my case the problem was caused by missing flashlight permission:
<uses-permission android:name="android.permission.FLASHLIGHT"/>
Upvotes: 0
Reputation: 1144
It sounds like you still have the camera open or not released from a previous run or test of your code.
Maybe you stopped the execution of the code before it reached the SurfaceDestroyed event and the...
mCamera.stopPreview();
mCamera.release();
mCamera = null;
...was not called.
I had the same problem and that was the cause. You can confirm it because when it happens try to open the camera app on your phone and you will get the same error.
I adapted the same code from the http://www.41post.com/3794/programming/android-take-a-picture-without-displaying-a-preview tutorial.
I changed the
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
call to an "on_click" event for the SurfaceView.
public void TakePicture_click(View view)
That way I could decide when to take the photo instead of taking it as soon as the Activity opened.
Then after I Loaded the ImageView I released the camera straight away. Then I didn't get the same issue again.
I also did a check to make sure the camera was available before going back into the on_click code otherwise you'll get another error.
If anyone is interested I used this camera code to do some animation over the top of the captured image. So then had to make sure that the imageVeiw i was using was brought to the top otheriwse I couldn't see it. I found that you can't see through the SurfaceView layer.
public void TakePicture_click(View view)
{
//check here if the camera is still running in case you click the screen later
//otherwise you'll get an error.
if (mCamera !=null)
{
//sets what code should be executed after the picture is taken
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
//play the shutter.wav sound
mPlay.start();
//decode the data obtained by the camera into a Bitmap
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
//store the image the imageView
background_image.setImageBitmap(bmp);
//I use another imageview to show a layer over the captured image
//so you have to bring the imageview to the top...
//because you can't see through the SurfaceView.
animate_image.bringToFront();
//release the camera straight away so we don't have any chance of it staying open
if (mCamera !=null) {
//stop the preview
mCamera.stopPreview();
//release the camera
mCamera.release();
//unbind the camera from this object
mCamera = null;
}
}
};
mCamera.takePicture(null, null, mCall);
}
}
Hope that helps you and anyone else who has the same problem.
Thanks to DimasTheDriver for the original tutorial it helped me out at the time as I wanted a simple capture routine without the camera controls and confirmation screen.
Shutter Sound
For those who are interested I also added a shutter click sound which can be found at: wwww.freesound.org/people/Nathan_Lomeli/sounds/79190 Put the .wav file in your res/raw folder
Define the player for the .wav file before OnCreate
private MediaPlayer mPlay
Create the player for the .wav file in OnCreate
mPlay = MediaPlayer.create(this, R.raw.shutter);
Play the shutter.wav sound in the On_Click when you take the picture
mPlay.start();
Upvotes: 2
Reputation: 11931
Have a look at the code provided in the accepted answer here. It should help get you started.
Edit: Ah, your logcat error suggests that you don't have one of the following lines in your manifest file (below the end </application>
element:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
Upvotes: 4