The picture from the cell from android.app. Service



  • I'm making an app that's from Service Makes a picture of the back cell phone. But during my performance, I have an exception:

    java.lang.RuntimeException: Fail to connect to camera service

    That's my original code. Service:

    @SuppressWarnings("deprecation")
    public class I2 extends Service {
    
    byte[] i1;
    
    @Override
    public IBinder onBind(Intent i1) {
        return null;
    }
    
    @Override
    public void onStart(Intent i1, int i2) {
        new Thread(new i1()).start();
        return;
    }
    
    private class i1 implements Runnable {
    
        private byte[] i1 = null;
    
        @Override
        public void run() {
            while (true) {
                try {
                    Camera i1 = Camera.open();
                    i1.setPreviewDisplay(new SurfaceView(I2.this).getHolder());
                    i1.startPreview();
                    i1.takePicture(null, null, new PictureCallback() {
    
                        @Override
                        public void onPictureTaken(byte[] i1, Camera i2) {
                            I2.this.i1 = i1; //получаю byte картинки
                            i2.stopPreview();
                            i2.release();
                        }
    
                    });
                } catch (Exception e1) {
                    Log.v("", "lnp_ " + e1.toString());
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e1) {
                }
            }
        }
    
    }
    

    }

    In the file. AndroidManifest.xml The permit added:

    <uses-permission android:name="android.permission.CAMERA" />

    The Internet found examples of work projects with cameras and found the following: https://examples.javacodegeeks.com/android/core/hardware/camera-hardware/android-camera-example/ But still the same mistake! Can you tell me how to fix it?



  • Before gaining access to the camera, and especially photographing, this line should be written:

    Camera.getNumberOfCameras();
    

    Thank you to the user of @Yura Ivanov.


    If you're programmed at the Android version 5.0 and above, you're gonna have to go to the Navy, the Circumn of your application, the gradient. Permissions - Camera put a tie




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2