Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Take picture without preview

I am trying to take a picture without preview, immediately when my application starts running and after that to save the picture in new folder - "pictures123", in the root folder. Could someone please tell me what's wrong in my code?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    File directory = new File(Environment.getExternalStorageDirectory() + "/pictures123/");

    if (!directory.exists()) {
        directory.mkdir();
    }

    Camera camera = Camera.open(0);
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPictureFormat(ImageFormat.JPEG);
    camera.setParameters(parameters);
    SurfaceView mview = new SurfaceView(getBaseContext());
    camera.setPreviewDisplay(mview.getHolder());
    camera.setPreviewDisplay(null);
    camera.startPreview();
    camera.takePicture(null,null,photoCallback);
    camera.stopPreview();
}

Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {

        try {
            String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root + "/pictures123");
            File file = new File (myDir, "pic1.jpeg");
            FileOutputStream out = new FileOutputStream(file);
            out.write(data);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        finish();

    }
};

permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
like image 565
Eyal Avatar asked Sep 09 '14 22:09

Eyal


People also ask

How do you get a still picture from a video on Android phone?

In the Photos app, pause the video and tap on the Get photos from this video button. Choose the still that has the text you want and tap on Export frame. At the bottom of the screen, you'll now see a collection of stills from the video.


1 Answers

You can't take a picture without a preview, but you don't have to show the preview on screen. You can direct the output to a SurfaceTexture instead (API 11+).

See this answer for more details.

like image 152
fadden Avatar answered Sep 29 '22 16:09

fadden