From b66ecf876963c11e6a699816389801f53b15f011 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 13 Apr 2016 20:44:03 +0200 Subject: [PATCH] add Camera open / release --- app/src/main/AndroidManifest.xml | 11 +++++- .../camera/MainActivity.java | 38 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c1300cae..1f5535a4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,13 +2,21 @@ + + + + - + @@ -16,5 +24,4 @@ - diff --git a/app/src/main/java/com/simplemobiletools/camera/MainActivity.java b/app/src/main/java/com/simplemobiletools/camera/MainActivity.java index ac8f1ae3..f7af2b33 100644 --- a/app/src/main/java/com/simplemobiletools/camera/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/camera/MainActivity.java @@ -1,13 +1,49 @@ package com.simplemobiletools.camera; -import android.support.v7.app.AppCompatActivity; +import android.hardware.Camera; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; public class MainActivity extends AppCompatActivity { + private static final String TAG = MainActivity.class.getSimpleName(); + private Camera camera; + private boolean isOpen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } + + @Override + protected void onResume() { + super.onResume(); + openCamera(); + } + + @Override + protected void onPause() { + super.onPause(); + releaseCamera(); + } + + private void openCamera() { + isOpen = false; + + try { + releaseCamera(); + camera = Camera.open(); + isOpen = (camera != null); + } catch (Exception e) { + Log.e(TAG, "openCamera exception " + e.getMessage()); + } + } + + private void releaseCamera() { + if (camera != null) { + camera.release(); + camera = null; + } + } }