Just to be clear: this isn't about a function returning null. It's about a function throwing an Exception which turns out to be null.
throw null;
Like that.
I have a camera application which has been working fine in Ice Cream Sandwich for a while. In Gingerbread it collapses; however, I have just seen Camera.open() throw null.
try {
releaseCameraAndPreview();
mCamera = Camera.open(camera);
//and the above throws ...
} catch (Exception e) {
e.printStackTrace();
//and e is null, causing a NullPointerException
}
releaseCameraAndPreview is this:
private void releaseCameraAndPreview() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
The camera is null at this point so nothing happens.
I didn't even know it was possible to throw null. What does it mean?
Restarting the phone fixed this; I can only assume the camera got into a corrupted state somehow during testing.
Re-locks the camera to prevent other processes from accessing it. Camera objects are locked by default unless unlock() is called. Normally reconnect() is used instead.
Since API level 14, camera is automatically locked for applications in start(). Applications can use the camera (ex: zoom) after recording starts. There is no need to call this after recording starts or stops.
Check if object is locked and try unlocking it if so.
https://developer.android.com/reference/android/hardware/Camera.html#lock%28%29
Related
I am learning how to play audio using MediaPlayer from this tutorial, which suggests using release() instead of stop() to STOP the audio. His explanation makes sense to me (free up the system resource as soon as you don't need it) and from a user perspective it works as expected, but I still feel like a bit weird that what's the point of using stop()? (https://stackoverflow.com/a/20580149/3466808)
fun stopPlayer1() = mediaPlayer?.stop()
fun stopPlayer2() {
mediaPlayer?.release()
mediaPlayer = null
}
So, which approach is better? Release as soon as user stops the audio? Or release only when the screen is no longer visible (onStop() called)?
take a look at the diagram in DOCS
MediaPlayer after release() is not "usable" anymore, you can nullify it safely. after onStop you still can call e.g. prepareAsync() and start playing again using single instance
edit: to comment
if (mMediaPlayer != null) {
try {
mMediaPlayer.stop();
} catch (Exception ignored) {
}
try {
mMediaPlayer.reset();
} catch (Exception ignored) {
}
try {
mMediaPlayer.release();
} catch (Exception ignored) {
}
mMediaPlayer = null;
}
I am creating a Screen Capturing App. Using MediaRecorder with MediaProjection API.
Getting Run-time Exception while stopping the recorder
Here is the code to stop Screen Capture
private void stop_recorder() {
if (virtualDisplay == null) {
return;
}
virtualDisplay.release();
if (mediaProjection != null) {
mediaProjection.unregisterCallback(projectionCallback);
mediaProjection.stop();
mediaProjection = null;
}
if (recorder != null) {
recorder.stop();
recorder.reset();
}
}
The Exception occur is
E/MediaRecorder: stop failed: -1007
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
The Issue is with the emulator. It does not have audio and video source.
Look android.developer doc
RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start().
Solution
Put the mediaRecorder.stop() function in try-catch block
I have a live wallpaper that just draws triangles, very simple, and runs smoothly all time. After testing found that when I open the camera to take pictures the phone freezes for about 10 seconds, then the camera opens and the wallpaper crashes and the message "Unfortunately, Live Wallpaper has stopped." appears. Also found that when I open any barcode scanner app the problem raises again because the scanner uses the camera too. Seems the problem raises when the camera app runs. Any ideas what's causing it?
hi #Bullet Camera is just open ones, if it open through any other apps then, you can can not access your camera, Solution is that, where you use camera please release that after no used.
main problem is that, in your app (app in that you are using camera) , so , you release camera after used.
like following :
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
when you need
private void releaseCameraAndPreview() {
if (camera != null) {
camera.release();
camera = null;
}
}
or
public void stopCamera() {
if (cameraDevice != null) {
cameraDevice.stopPreview();
cameraDevice.setPreviewCallback(null);
cameraDevice.release();
cameraDevice = null;
System.out.println("in to the stop video");
}
}
My app is using camera. i'm having a CameraActivity class and CameraFragment class,
CameraFragment is responsible for releasing the camera:
#Override
public void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
My app crashed while taking a picture. for some reason the camera was not released. now i can't get the camera object nowhere! Also the android Camera app is not working with an error "Can't connect to the camera"
now i know that the camera is a singleton object and only one app can get it in a time.
what i don't understand is why onPause didn't run and how can i protect the camera instance better?
Also if something like that happens, how can i force release the camera object? it it a good idea?
sadly i don't have the information why the app crashed at the first place.
Thanks for your help.
Roy
This should help:
private static void unCaughtExceptionHandler() {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(final Thread thread, final Throwable ex) {
ex.printStackTrace();
releaseCamera();
System.exit(0);
}
});
}
I'm using the Android APIDemo sample code.
When I run the CameraPreview example, at first it was giving me an error.
I traced that one down and the sample was working for a while.
Now, it no longer works. It says
ERROR/AndroidRuntime(2949): java.lang.RuntimeException: Fail to connect to camera service
What can be causing that? It happens when camera.open() is called.
Thanks,
Tee
Be sure to properly release all the aquired camera resources:
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCam != null) {
mCam.stopPreview();
mCam.setPreviewCallback(null);
mCam.release();
mCam = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCam == null) {
mCam = Camera.open();
try {
mCam.setPreviewDisplay(holder);
// TODO test how much setPreviewCallbackWithBuffer is faster
mCam.setPreviewCallback(this);
} catch (IOException e) {
mCam.release();
mCam = null;
}
}
}
Make sure your <uses-permission> elements are in the proper positions in your AndroidManifest.xml file.
It happens if your activity does not close the camera properly in surfaceDestroyed or onConfigurationChanged etc...
Don't forget to do this everytime you go out of your activity:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
Another reason of this error is when you try to open camera but some other application or even your application is already using camera.
I also get this type of issue on a HTC device. To solve add this code:
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (camera!=null)
{
camera.stopPreview();
camera.release();
camera=null;
}
}
And yet you cannot start camera then restart device.
Also, if you are using the emulator, make sure you have selected to Emulate the Front Camera and/or the Back Camera.
Android Virtual Device Manager->Select Device->Edit->Front Camera->Emulated
As others mention, you have to call release() on your camera object when you're finished.
I wasn't doing this initially, so I changed my code but it still gave me the same error. I was deploying directly to a physical handset and had to restart the phone before it worked
I also received this error when I was testing and stopped execution before reaching the point in code when the:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
was called. This then blocked the camera because it hadn't een released properly. My solution was to turn the camera off and back on again. You can confirm this is the case by trying to use the inbuilt Camera app in your phone. It won't work either because it is still busy.
Second #matt-burns however you might want to check that you're only trying to get the camera once. I had forgotten to comment out a line and was trying to launch two activities that would both try to obtain the camera.