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();
}
});
}