有没有办法在没有预览的情况下获取照片并将其放入android sdk 19的数据库中 在下面的代码中,每隔5分钟自动获取位置并在地图上放置标记,我需要自动拍照并将其放入数据库
public void startCountDown() {
if (this._countDownTimer != null) {
this._countDownTimer.cancel();
}
_countDownTimer = new CountDownTimer(60000 * 5, 60000*5) {
@Override
public void onFinish() {
}
@Override
public void onTick(long millisUntilFinished) {
putMarkerToLocation();
takePhotoAndSaveIt2Db(); // ==> here what i need
}
}.start();
}
答案 0 :(得分:0)
以下服务在api19上完美运作
public class CapPhoto extends Service { private Camera mCamera; @Override public void onCreate() { super.onCreate(); Log.d("CAM", "start"); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } Thread myThread = null; } @Override public void onStart(Intent intent, int startId) { takePhoto(); } @Override public IBinder onBind(Intent intent) { return null; } private void takePhoto() { System.out.println("Fotoraf Cekimi Hazirligi Basladi"); Camera camera = null; int cameraCount = 0; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); for (int camIdx = 0; camIdx < cameraCount; camIdx++) { SystemClock.sleep(1000); Camera.getCameraInfo(camIdx, cameraInfo); try { camera = Camera.open(camIdx); } catch (RuntimeException e) { System.out.println("Camera not available: " + camIdx); camera = null; //e.printStackTrace(); } try { if (null == camera) { System.out.println("Could not get camera instance"); } else { System.out.println("Got the camera, creating the dummy surface texture"); //SurfaceTexture dummySurfaceTextureF = new SurfaceTexture(0); try { //camera.setPreviewTexture(dummySurfaceTextureF); camera.setPreviewTexture(new SurfaceTexture(0)); camera.startPreview(); } catch (Exception e) { System.out.println("Could not set the surface preview texture"); e.printStackTrace(); } camIdx = cameraCount; Camera.Parameters params = camera.getParameters(); params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO); params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO); params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO); params.setExposureCompensation(0); params.setPictureFormat(ImageFormat.JPEG); params.setJpegQuality(100); params.setRotation(90); camera.setParameters(params); camera.takePicture(null, null, new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { File pictureFileDir =new File(Environment.getExternalStorageDirectory(), "A"); if(!pictureFileDir.exists()){ pictureFileDir.mkdirs(); } // File pictureFileDir = getDir(); if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { return; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); String date = dateFormat.format(new Date()); String photoFile = "TaksiResim_" + "_" + date + ".jpg"; String filename = pictureFileDir.getPath() + File.separator + photoFile; File mainPicture = new File(filename); //addImageFile(mainPicture); try { FileOutputStream fos = new FileOutputStream(mainPicture); fos.write(data); fos.close(); System.out.println("resim kayit edildi"); } catch (Exception error) { System.out.println("resim kayit edilemedi"); } camera.release(); } }); } } catch (Exception e) { camera.release(); } } } }