ImageCaptureException: Camera is not active. message from takepicture

421 views
Skip to first unread message

Main Dsk

unread,
Feb 26, 2022, 5:12:52 AM2/26/22
to Android CameraX Discussion Group
I have a LG stylo5 with Android 9

Trying to take a picture in a service

My code here

public class PLayerService extends LifecycleService {
 
ImageCapture imageCapture = null;
Executor executor;
private final ServiceLifecycleDispatcher mDispatcher = new ServiceLifecycleDispatcher(this);

private File videoDir;
private File path;
@Override
public void onCreate() {
mDispatcher.onServicePreSuperOnCreate();
super.onCreate();
}
// camera stuff
@RequiresApi(api = Build.VERSION_CODES.N)
@Override

//public void onCreate() {
public int onStartCommand(Intent intent, int flags, int startId) {

super.onStartCommand(intent, flags, startId);


createNotificationChannel();
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification notification = new NotificationCompat.Builder(this, "ChannelID1")
.setContentTitle("Big App")
.setContentText("Application Ongoing")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent).build();
startForeground(1, notification);
start_Camera();  Here is where I started the camera

return START_STICKY;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
"ChannelID1", "Foreground Notification",
NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}
//camera stuff
@NonNull
@Override
public Lifecycle getLifecycle() {
return mDispatcher.getLifecycle();
}



@RequiresApi(api = Build.VERSION_CODES.N)
public void start_Camera() {

ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
ProcessCameraProvider.getInstance(this);



cameraProviderFuture.addListener(() -> {
try {

ProcessCameraProvider cameraProvider = cameraProviderFuture.get();

CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
.build();

executor = Executors.newSingleThreadExecutor();

ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
//.setTargetResolution(new Size(width,height))
.setBackgroundExecutor(executor)
.setBackpressureStrategy(ImageAnalysis.STRATEGY_BLOCK_PRODUCER)
.setImageQueueDepth(5)
.build();

imageAnalysis.setAnalyzer(executor, image -> {
int rotationDegrees = image.getImageInfo().getRotationDegrees();



image.close();
});


imageCapture = new ImageCapture.Builder().setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.build();
// Attach use cases to the camera with the same lifecycle owner
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, imageCapture);
savePicture();

} catch (InterruptedException | ExecutionException e) {
// Currently no exceptions thrown. cameraProviderFuture.get()
// shouldn't block since the listener is being called, so no need to
// handle InterruptedException.
}
}, ContextCompat.getMainExecutor(this));



audio ("device_connected");



}



@RequiresApi(api = Build.VERSION_CODES.N)
public void savePicture() {


// file folder
Date currentTime = Calendar.getInstance().getTime();
currentTime.toString();


videoDir = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/");
if (!videoDir.exists()) {
videoDir.mkdir();
}

String fileName = currentTime + ".jpg";
path = new File(videoDir, fileName);
//file folder
if (this.imageCapture == null) {
start_Camera();
return;
}

ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(path).build();

// Set up the capture use case to allow users to take photos
imageCapture.takePicture(outputFileOptions, this.executor, new ImageCapture.OnImageSavedCallback () {

@Override

public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
 
}
});
}

@Override
public void onError(@NonNull ImageCaptureException error) {
error.printStackTrace();
}


});

}

fake name

unread,
Feb 26, 2022, 2:22:40 PM2/26/22
to Android CameraX Discussion Group
I got rid of all the life cycle references  mDispatcher = new ServiceLifecycleDispatcher(this), and kept only the 

public class PLayerService extends LifecycleService {

I got some weird results

image.png
The Camera is now open I suppose but I dont see any pictures saved even though it did send the capture request but nothing is saved

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/c4b50b72-9292-46a8-aa07-0190fdfc39f2n%40android.com.

fake name

unread,
Feb 27, 2022, 2:49:50 PM2/27/22
to Android CameraX Discussion Group

Tonality App

unread,
Jul 12, 2022, 2:56:07 PM7/12/22
to Android CameraX Discussion Group, Main Dsk
Note to anyone else with the same issue: make sure to call `super.onStartCommand(intent, flags, startId)`. Unlike a normal service where this is a no-op, in a LifecycleService this moves the lifecycle state to `STARTED`, which enables the camera to open when you call `bindToLifecycle` on the `ProcessCameraProvider`.
Reply all
Reply to author
Forward
0 new messages