-1

I found this Question but it doesn't work for me. (Nothing happens. Tested on Wiko Rainbow Jam) Android - Camera2 : The easiest way to turn on the torch light

My App have to run on min. API Level 16! Is there an SupportCameraManager or an Library (under Apache), which can I use?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user3853134
  • 142
  • 1
  • 11

2 Answers2

1

You Can Use this.

Initialize the NoobCameraManager singleton.

NoobCameraManager.getInstance().init(this);

You can optionally set the Log Level for debug logging. Logging uses LumberJack library. The default LogLevel is LogLevel.None

NoobCameraManager.getInstance().init(this, LogLevel.Verbose);

After that you just need to call the singleton to turn on or off the camera flash.

NoobCameraManager.getInstance().turnOnFlash();
NoobCameraManager.getInstance().turnOffFlash();

You can take care of the runtime permission to access Camera yourself or can allow the library to do it for you

NoobCameraManager.getInstance().takePermissions();

Note: The library will take permissions, if you haven't already, even without calling takePermissions() explicitly. This behavior may change in future.

It's easy to toggle Flash too

NoobCameraManager.getInstance().toggleFlash();

It's a good practice to release all the resources, once you're done.

NoobCameraManager.getInstance().release();
Syed Ali Naqi
  • 701
  • 7
  • 15
  • Yes I had found this Library too, but I always get this Error Message: (Edit: Error Message is too long for this comment -> https://pastebin.com/FwqZhxXZ ) – user3853134 Sep 15 '17 at 21:48
  • You wouldn't If you asked User for Permission and Initiated the Library... – Syed Ali Naqi Sep 15 '17 at 21:50
  • I init the Library via EasyFlashlight.init(this); in Constructor and ask for the following Permissions in Manifest.xml: android.permission.CAMERA android.permission.FLASHLIGHT android.hardware.camera android.hardware.camera.autofocus android.hardware.camera.flash – user3853134 Sep 15 '17 at 21:52
  • if(EasyFlashlight.getInstance().canAccessFlashlight) { //now you can interact safely with the Flashlight API } – Syed Ali Naqi Sep 15 '17 at 21:55
  • Because Android Studio says: "cannot resolve symbol 'canAccesFlashlight'" I'd added two Brackets so my If-Statement looks like that: if(EasyFlashlight.getInstance().canAccessFlashlight()) { EasyFlashlight.getInstance().turnOn(); } //Edit: Still cause the Error – user3853134 Sep 15 '17 at 21:56
  • I replaced this library in my project with the old one. Nevertheless, I received the same error. Afterwards I downloaded the official demo project of NoobCameraFlash and opened it in Android Studio. I wanted to test this project on my Wiko Rainbow Jam (Android 5.1) but I get the same bug here, too. Is there something wrong with my phone? – user3853134 Sep 15 '17 at 22:24
  • wait... Your phone does have a flash right? what is the model? – Syed Ali Naqi Sep 15 '17 at 22:27
  • Of course, my mobile phone has a camera and lamp on the back. http://de.wikomobile.com/m834-rainbow-jam (Edit: And the camera (including flash light) works with Facebook, Instagramm, etc. Apps) – user3853134 Sep 15 '17 at 22:33
  • The Error seems to be the same. `Bad argument passed to camera service` meaning you are doing something wrong.. I have used this library. and it works fine for me.. On the other hand you can always try using the link u mentioned in ur question. – Syed Ali Naqi Sep 15 '17 at 22:42
  • I can't imagine I'm doing anything wrong because I downloaded and opened the example project from NoobCameraManager. This works as already mentioned, not even with my mobile phone. Tomorrow I will try to start the app on other mobile phones, because I don't have any other mobile phones available at the moment. If that doesn't help, I'll probably have to google for a long time. ; D And once again: In normal use, the flash and camera work. Thanks for your help anyway! – user3853134 Sep 15 '17 at 22:57
  • Hey Syed Ali Naqi, I was able to solve the problem with a code snippet. – user3853134 Sep 18 '17 at 13:57
  • Great! what was the Problem? – Syed Ali Naqi Sep 18 '17 at 19:36
1

SOLUTION: The problem was that I only got the camera's parameters when turning on the light and turning off the light. This must apparently also be done in the constructor or in the overwriting onStart method.

@Override
protected void onStart() {
    super.onStart();

    camera = Camera.open(); //Also Call this
    params = camera.getParameters(); //and this, in the Constructor
}

as fields:

private Camera camera;
Parameters params;

And then you can start the flashlight with these snippets of code: Android - Camera2 : The easiest way to turn on the torch light

user3853134
  • 142
  • 1
  • 11