Simple Android Flashlight app
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
One of my first projects. I present to you - Android Flashlight app!
public class MainActivity extends AppCompatActivity
Camera cam = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void onBtn(View view)
if (checkCameraHardware(this))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
System.out.println("Check : has permission");
public void handleFlash()
if (cam == null)
cam = getCameraInstance();
Camera.Parameters parameters = cam.getParameters();
if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(parameters);
cam.startPreview();
else if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camEnd();
public static Camera getCameraInstance()
Camera camera = null;
try
camera = Camera.open(); // attempt to get a Camera instance
catch (Exception e)
// Camera is not available (in use or does not exist)
e.printStackTrace();
return camera; // returns null if camera is unavailable
private boolean checkCameraHardware(Context context)
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return true;
else
return false;
public void camEnd()
cam.release();
cam = null;
@Override
protected void onDestroy()
super.onDestroy();
camEnd();
I have tested this on my Marshmallow device and it works as expected (but no on my lollipop device for some reason). Any suggestions would be great!
java beginner android
add a comment |Â
up vote
0
down vote
favorite
One of my first projects. I present to you - Android Flashlight app!
public class MainActivity extends AppCompatActivity
Camera cam = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void onBtn(View view)
if (checkCameraHardware(this))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
System.out.println("Check : has permission");
public void handleFlash()
if (cam == null)
cam = getCameraInstance();
Camera.Parameters parameters = cam.getParameters();
if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(parameters);
cam.startPreview();
else if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camEnd();
public static Camera getCameraInstance()
Camera camera = null;
try
camera = Camera.open(); // attempt to get a Camera instance
catch (Exception e)
// Camera is not available (in use or does not exist)
e.printStackTrace();
return camera; // returns null if camera is unavailable
private boolean checkCameraHardware(Context context)
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return true;
else
return false;
public void camEnd()
cam.release();
cam = null;
@Override
protected void onDestroy()
super.onDestroy();
camEnd();
I have tested this on my Marshmallow device and it works as expected (but no on my lollipop device for some reason). Any suggestions would be great!
java beginner android
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
One of my first projects. I present to you - Android Flashlight app!
public class MainActivity extends AppCompatActivity
Camera cam = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void onBtn(View view)
if (checkCameraHardware(this))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
System.out.println("Check : has permission");
public void handleFlash()
if (cam == null)
cam = getCameraInstance();
Camera.Parameters parameters = cam.getParameters();
if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(parameters);
cam.startPreview();
else if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camEnd();
public static Camera getCameraInstance()
Camera camera = null;
try
camera = Camera.open(); // attempt to get a Camera instance
catch (Exception e)
// Camera is not available (in use or does not exist)
e.printStackTrace();
return camera; // returns null if camera is unavailable
private boolean checkCameraHardware(Context context)
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return true;
else
return false;
public void camEnd()
cam.release();
cam = null;
@Override
protected void onDestroy()
super.onDestroy();
camEnd();
I have tested this on my Marshmallow device and it works as expected (but no on my lollipop device for some reason). Any suggestions would be great!
java beginner android
One of my first projects. I present to you - Android Flashlight app!
public class MainActivity extends AppCompatActivity
Camera cam = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void onBtn(View view)
if (checkCameraHardware(this))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
System.out.println("Check : has permission");
public void handleFlash()
if (cam == null)
cam = getCameraInstance();
Camera.Parameters parameters = cam.getParameters();
if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(parameters);
cam.startPreview();
else if (parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camEnd();
public static Camera getCameraInstance()
Camera camera = null;
try
camera = Camera.open(); // attempt to get a Camera instance
catch (Exception e)
// Camera is not available (in use or does not exist)
e.printStackTrace();
return camera; // returns null if camera is unavailable
private boolean checkCameraHardware(Context context)
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return true;
else
return false;
public void camEnd()
cam.release();
cam = null;
@Override
protected void onDestroy()
super.onDestroy();
camEnd();
I have tested this on my Marshmallow device and it works as expected (but no on my lollipop device for some reason). Any suggestions would be great!
java beginner android
edited May 30 at 6:23
Mast
7,32863484
7,32863484
asked Mar 23 at 12:07
Bar Akiva
1215
1215
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
Your inner if-else
is incorrect, and that's why it's not working under Marshmallow
.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
System.out.println("Check : has permission");
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Your inner if-else
is incorrect, and that's why it's not working under Marshmallow
.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
System.out.println("Check : has permission");
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
add a comment |Â
up vote
2
down vote
Your inner if-else
is incorrect, and that's why it's not working under Marshmallow
.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
System.out.println("Check : has permission");
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Your inner if-else
is incorrect, and that's why it's not working under Marshmallow
.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
System.out.println("Check : has permission");
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
Your inner if-else
is incorrect, and that's why it's not working under Marshmallow
.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
System.out.println("Check : has permission");
ActivityCompat.requestPermissions(this, new StringManifest.permission.CAMERA, 1);
handleFlash();
else
handleFlash();
answered Jun 14 at 13:53
Jabbar_Jigariyo
22414
22414
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190298%2fsimple-android-flashlight-app%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password