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" />