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" />
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With