Im currently trying to activate the built in front flashlight on a Samsung Galaxy A6, but the device provides information like there is no built in front flash.
I've already tried different methods, which can be found by searching for activating flash.
First Try was to get supportedFlashModes and then activate the flash by using setParameters (API < 23). But simply the getParameters() for the front camera doesn't return any information of the built in front flash. For the app it just seems like there is no front flash available.
Then i tried to use the Camera2 API, introduced in API >= 23 and there the same Problem occurs. Fetching camera characteristics and then check if FLASH_INFO is available just results in returning false. Also trying to just activate the flash unit with setTorchMode(FRONT_CAMERA, true) throws an exception which says: No flash unit available.
I currently only have Samsung Galaxy A6 as test device with built in front flash. The same code works fine for the rear (back) camera without any problems.
ad 1)
try {
Camera camera;
camera = Camera.open(cameraId);
if (camera == null) {
return false;
}
Camera.Parameters parameters = camera.getParameters();
if (parameters.getFlashMode() == null) {
camera.release();
return false;
}
...
It just quits at that point because .getFlashMode() returns null for the front camera. Next steps would be to check the supportedFlashModes and then call setParamater of Camera.
ad 2)
try {
String camID = null;
for(String cameraID : mCameraManager.getCameraIdList()) {
CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(cameraID);
int orientation = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
if (orientation == CameraCharacteristics.LENS_FACING_FRONT) {
if(cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
camID = cameraID;
}
}
if(camID != null) {
mCameraManager.setTorchMode(camID, true);
}
} catch (Exception exc) {
...
}
These are only snippets for simply activating the front flash, but both methods act like the hardware response without a built in front flash.
After research it seems that some phone manufacturers are using private APIs to control hardware. Especially Samsung and Huawei are making use of that. The main camera app on a device is mainly controlled by that API and Frontflash can be used as expected. But even big camera apps like Instagram and Snapchat can't get rid of that and are also not able to activate e.g. Frontflash.
For own purposes the hardware just doesn't provide any features for activating Frontflash on some devices, it just reacts like there is no built-in frontflash. For now there are no solutions available for this problem.
Exisiting workarounds are doing a whitening of the screen to lighten up the environment in front of the phone.
Related
halo,
i have an issue with my app i'm trying to get all cameras of my phone.
my app detects only 2 cameras
i'm using :
val manager = context!!.getSystemService(Context.CAMERA_SERVICE) as CameraManager
var listOfCamer as = manager.cameraIdList;
When I tried openCamera (https://opencamera.sourceforge.io/help.html), it detects 4 cameras!
Or we use the same method/class
CameraManager manager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
try {
return manager.getCameraIdList().length;
}
am I missing some config / permissions?
(Android 10 or higher) Hide physical sub-cameras from getCameraIdList. This reduces the number of cameras that can be directly opened by apps, eliminating the need for apps to have complex camera selection logic.
I think this is the reason. Try the same with old Camera API (though it is deprecated now), perhaps, it would allow you to see all your 4 devices.
I am working on android project, where NFC is used as a communication. I am facing a weird problem, when mobile device has a NFC, it is enabled, but it is not working on some devices (adapter is not enabled when debugging). I am writing logs and it prints, NFC on, adapter disabled.
For example: HTC One m9(os 7.0). Also happens with OnePlus One(os 9)! But again, it works on other devices.
Did you experience the same issue?
Here is some code:
object NfcUtil {
fun getNfcAdapter(c: Context): NfcAdapter? {
val manager = c.getSystemService(Context.NFC_SERVICE) as NfcManager
return manager.defaultAdapter
}
fun doesSupportHce(c: Context): Boolean {
return c.packageManager.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
}
}
val adapter = NfcUtil.getNfcAdapter(this)
if (adapter != null && NfcUtil.doesSupportHce(this)) {
if (adapter.isEnabled) {
tvNfcOff.extHide()
} else {
tvNfcOff.extShow()
}
}
I think that if NFC is supported and enabled but the adapter is disabled (https://developer.android.com/reference/android/nfc/NfcAdapter#isEnabled()) I'll follow the guidelines and redirects the user to the settings screen with the intent mentioned in the documentation.
If the user come back few times you could monitor it and show a different message instead of redirecting to settings, something like: NFC is not working properly on your device. I'd check if you have lots of users using those devices, if yes, I will try to research more on the Operating System and Device having this issue.
And later on I will just try to debug it with that Device and that specific Operating System that is having this kind of issue. I'll try to see if other apps using NFC has same issues or they work fine, and by work fine I mean that the communication happens not that other apps dont show any warning/error popup message.
And if I found out its an issue in a specific OS Version, also with other apps, I'll just try to inform the users and get an update on which version the issue have been fixed. Otherwise if other apps can make a successful NFC communication in that device/OS that is not working for me, I'll just dig deeper.
For now I can say there is nothing wrong in your implementation and looks good.
It might be an issue with the current OS or if you have any Custom ROM that might not fully support or have a functional NFC driver.
Two additional bits of info that might be useful
1) Use a Broadcaster receiver to get notified when the NFC state changes, because using the quick settings pull down does not pause your app, therefore retesting nfc status in onResume does not work (a user changing via the full settings app will pause you App, though)
Example of how to do it in Java
#Override
protected void onCreate(Bundle savedInstanceState) {
// All normal onCreate Stuff
// Listen to NFC setting changes
this.registerReceiver(mReceiver, filter);
}
// Listen for NFC being turned on while in the App
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
NfcAdapter.STATE_OFF);
switch (state) {
case NfcAdapter.STATE_OFF:
// Tell the user to turn NFC on if App requires it
break;
case NfcAdapter.STATE_TURNING_OFF:
break;
case NfcAdapter.STATE_ON:
// Do something with this to enable NFC listening
break;
case NfcAdapter.STATE_TURNING_ON:
break;
}
}
}
};
2) Don't assume that the device has a NFC settings page, if your app works with and without NFC, if the adapter is null don't assume you can start an Intent to the NFC settings page as suggested by #denis_lor as this will cause a crash if the OS does not have a NFC adapter to turn on.
I would like to know if there is some kind of an API in android studio that enables communication between a device and the android phone through a USB. For example, external camera.
I have used SetupAPI and WINUSB before to accomplish such a task. So something similar to those two would be appreciated.
The company that created the device does not provide an SDK, driver or any extra information.
Thank you very much.
It depends on what you want to do BUT short answer is yes.
To detect an external camera you may try this:
public String getExternalCamera(){
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
String exCamId = null;
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
//LENS_FACING_EXTERNAL will return Value: 2
if (facing != null && facing.equals(CameraCharacteristics.LENS_FACING_EXTERNAL)) {
exCamId = cameraId;
}
}
return exCamId;
}
LENS_FACING_EXTERNAL
added in API level 23
public static final int LENS_FACING_EXTERNAL
The camera device is an external camera and has no fixed facing relative to the device's screen.
You can also use:
INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL
added in API level 28
public static final int INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL
This camera device is backed by an external camera connected to this Android device.
The device has capability identical to a LIMITED level device, with some exceptions.
For more info see Android documentation here!
I want to build an app that keeps the front LED flash/torch on while taking a picture. So I have the following code that opens the camera using an implicit intent:
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, requestImageCapture)
And the code for turning on and off the torch/flash of the phone:
if(isLightOn) {
val manager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
val cameraId = manager.cameraIdList[0]
manager.setTorchMode(cameraId, false)
isLightOn = false
} else {
val manager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
val cameraId = manager.cameraIdList[0]
manager.setTorchMode(cameraId, true)
isLightOn = true
}
I've set the listeners of 2 buttons to perform these actions. Though they work well on their own, the torch/flash does not stay on when the camera is opened with the intent. Is there any way by which I can achieve this behavior?
The code for the torch works, but it only works for your app. After the startActivityForResult(intent, requestImageCapture) is executed you are no longer in your app. You are in whatever camera app you select. Your app loses access to the camera and the Camera app gets it.
The flash now can be controlled for the Camera app. The camera app probably has controls for flash.
If you want to enable the flash and take a photo you'll have to create your own camera app. You can do it from scratch following this guide or you can use a camera library like Fotoapparat or material-camera
i use this method to record video from front camera:
Recording video via Mediarecorder
it works fine in my Nexus 4 but some people says that there is a lot of phones that their front camera cant record video and they only can take pictures. My Android App functionality based on recording video from front camera and my question is that is this true that some phones can't record video via front camera? and how i can detect this and inform user?
Try calling some code like this
CameraInfo cameraInfo = new CameraInfo();
if (cameraInfo.facing = CameraInfo.CAMERA_FACING_FRONT) {
//do your code?
} else {
//alert the user via toast or dialog
}
no built in way to figure it out though.
EDIT:
should work on API 9 and above.
maybe try calling these methods to first get a camera object, then check to see if there is a camcorderProfile available for the front facing camera?
hasProfile (int cameraId, int quality)
setCamera(camera);