I am trying to develop an app that would take picture when you light the screen.
I created different method to do so, and different logs. When I call the method supposed to take the picture : this is in a Service
public void photo() {
try {
cam.setPreviewDisplay(new SurfaceView(this).getHolder());
cam.startPreview();
cam.takePicture(null,null,new PhotoHandler());
Log.i("photo","end");
}catch(IOException e) {e.printStackTrace();}
}
In the logs, the "photo","end" is displayed but the logs I put in PhotoHandler are not.
I also have the "Camera" "app passed NULL Surface" log.
So I am asking you guys why is the picture not taken ? The PhotoHandler works fine, I tested it in a basic app.
Hope you'll help me !
EDIT : The camera is selected in the onCreate method of the service and gives no error.
EDIT : The problem was that my PhotoHandler wasn't called so camera.release() too. My photo() method looks like this now
public void photo() {
Camera cam=null;
try {
cam=Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
cam.setPreviewTexture(new SurfaceTexture(0));
cam.startPreview();
cam.takePicture(null,null,new PhotoHandler());
cam.stopPreview();
cam.release();
Log.i("photo","end");
}catch(IOException e) {e.printStackTrace();stopSelf();}
catch(RuntimeException re {e.printStackTrace();stopSelf();}
Now, the problem is that the takePicture method is called when the service end, must be a thread problem I assume. But what is more bothering is that the takePicture method is not always called after the service end. And when the method is called, it's called once, whereas I light the screen more than one time during the life of the service.
Related
I try to play some music for my activity but "sometimes" it throws exceptions below :
android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
This is my code that might be related on this error.
...
Thread thread = new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bgm.start();
});
thread.start();
...
This code is in onCreate method and rest part is just make some animations with Handler or get 3 String intent values and five int intent values from previous activity.
It doesn't even tell me which line of my code it happened.
Thanks for reading my question.
Possible Solutions:
Override your service's onDestroy() method and watch what event flow leads to it. If you catch DeadObjectException without going through this method, your service should have been killed by the OS.
by removing Typeface, this might be because of ttf which I was using from the assets folder Please try comment the typeface and test it hope it will work for sure
put all your code inside the onCreate. From there you will see what is the culprit like a NullPointerException for example but your code will run smoothly already.
DeadObjectException: The object you are calling has died, because its
hosting process no longer exists.
I'm writing an Android app and I need to take photos. I got it mostly working very quickly using the standard tutorial. But I need to write a blocking version of "takePicture()". I've trying reworking my design so that everything is done asynchronously, but the code just ends up as spaghetti code because so many tasks are dependent on the previous task completing. And I had expected that taking a photo would take less than a second. So I think it's a reasonable goal to have a blocking (synchronous) "takePicture()".
So I created a camera in the UI thread (I'm an Android newbie but I presume that 'onCreate()' et al are called in the UI thread). I also open the camera and release it in the UI thread. I also call "takePicture()" in the UI thread. I tried polling for a "volatile byte[] jpeg;" field to become non-null, with a "while (jpeg != null) sleep(1000);" but it never gets assigned. To my surprise, my callback doesn't get called until I pass out of the "onClick()" function - 'takePicture()' doesn't seem to trigger anything, it merely queues something, as far as I can figure out.
My next attempt involved doing all the camera work (open, takePicture, release) in a "ExecutorService.newSingleThreadExecutor()" but that was also unsuccessful. Even when I did it this way (see below), the callback wasn't called until the app finished the 'onClick()'.
public byte[] takePhoto()
{
if (camera == null)
return null;
cameraThreadService.submit(new Runnable() {
#Override
public void run() {
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
jpeg = data;
}
};
camera.takePicture(null, null, mPicture);
}
});
int n = 0;
while (jpeg == null) {
sleep(1000);
if (n++ > 5)
break;
}
return jpeg;
}
Can anyone tell me how to write a blocking (synchronous) 'camera.takePicture()'?
To my surprise, my callback doesn't get called until I pass out of the "onClick()" function - 'takePicture()' doesn't seem to trigger anything, it merely queues something, as far as I can figure out.
Your onPictureTaken() is called on the main application thread. The main application thread cannot do two things at once. onPictureTaken() cannot be called until after you return from onClick().
Can anyone tell me how to write a blocking (synchronous) 'camera.takePicture()'?
That is not possible, sorry.
I using android android.hardware.Camera class to capture image. For initialization I use following code.
Camera cam = null;
try {
cam = Camera.open();
} catch (Exception e) {
e.printStackTrace();
// camera unavailable
}
return cam;
I am interesting in catch block. For example, torch app is running at the moment of initialization. So Camera.open() throw RuntimeException with message Fail to connect to camera service.
Here is exception:
Is it possible to know the reason why camera is unavailable? I want to guide user how to fix this, for example show a dialog: "dude, turn off your torch".
If it's not possible, then in which cases camera could be blocked? I just will show user dialog with general instruction what he could do, for example: 1. turn off torch. 2. Close all camera app 3. Fed your cat etc.
The below flashlight code used to work properly earlier. but when I started the android IDE for next time. it shows NullPointerException Error in the specific line consist of Camera Parameters. I have tried every other conditions to trace the stack still I cant find any relevant solutions. Any help is appreciated.
I have also visited some of the related queries in the forum. one of which queries I have found useful but still It didn't work.
Google results
Flashlight.java
public void connectCameraService() {
if (camera == null) {
camera = android.hardware.Camera.open();
params = camera.getParameters(); //specific line shows NullPointerException Error.
}
}
The above method also creates Error in Override methods.
#Override
protected void onStart() {
super.onStart();
connectCameraService();
}
I am using pretty much the same code as the Camera app code from Google but am getting really weird results. Inside my application and the Camera app from Google the preview gets stuck or is overlid with weird lines. The preview is usually the last thing which I see in the phone's own Camera app.
The Samsung model is I9003. The same code ran fine on I9000 which Samsung just discontinued. The code also works fine on an HTC Wildfire.
Any resolution for this?
Just noticed that after taking a photo inside my application the camera preview becomes normal. Same thing is happening in the Google camera application.
Couldn't post the answer earlier. Wasn't sure if it was the right thing to do, but now with the app running properly on around 150 devices I guess this works.
So the Android camera app in its onCreate function had the following code:
/*
* To reduce startup time, we start the preview in another thread.
* We make sure the preview is started at the end of onCreate.
*/
Thread startPreviewThread = new Thread(new Runnable() {
public void run() {
try {
mStartPreviewFail = false;
startPreview();
} catch (CameraHardwareException e) {
// In eng build, we throw the exception so that test tool
// can detect it and report it
if ("eng".equals(Build.TYPE)) {
throw new RuntimeException(e);
}
mStartPreviewFail = true;
}
}
});
startPreviewThread.start();
For some reason this did not work on GT-I9003. What I noticed was that after taking a photo the preview would come properly so there was nothing wrong with the hardware as such. I tried to retrace what was happening after a photo was taken and then compare it with the code with which the camera was first initialized. I commented out this code from onCreate. The onResume from the camera app looked like this:
if (mSurfaceHolder != null) {
// If first time initialization is not finished, put it in the
// message queue.
if (!mFirstTimeInitialized) {
mHandler.sendEmptyMessage(FIRST_TIME_INIT);
} else {
initializeSecondTime();
}
}
I changed this to:
if (!mFirstTimeInitialized) {
initializeFirstTime();
} else {
initializeSecondTime();
}
There were some other changes too, will put it up on GitHub as a separate app soon.