I am using Android emulator and development platform.
I have created a new application and wonder how can I close or open Android cell phone camera?
If you are talking about opening it from your app you should use the camera Intent for that.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(intent, 1);
EDIT:
1 - is for the front-facing camera
When you create the AVD for your emulator, there are options for enabling front and back camera. If you have enabled this option for your emulator, you will be able to access the camera just like you would on a regular phone (using the camera app, or via the system).
You probably didn't enable the camera option on your emulator image if you are not seeing it.
I am going to assume you have enabled a camera on your emulator, and want to access it's functions yourself, instead of opening an existing camera application. You'll need to use functions in Android to actually access this hardware, and you'll also need to give the app permission to use it.
private void startCamera(){
//First check if a camera is available
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
Log.d("CameraApp", "It has a camera");
Camera cam = Camera.open(); //Start using the camera. From here on out you should be able to access it's functions.
cam.unlock();
cam.startPreview();
} else {
Log.d("CameraApp", "It does not have a camera");
Toast.makeText(this, "No camera available",
Toast.LENGTH_SHORT).show();
}
}
Be sure to close it when you're done.
private void stopCamera(){
cam.stopPreview();
cam.release();
}
Add this to your AndroidManifest.xml to give your app permission:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Each function (zoom, autofocus, etc.) needs it's own permission to be added.
Also see this for more information: http://developer.android.com/reference/android/hardware/Camera.html
Related
The expected behavior is that the app will be running all the time when it's in ON state. Some phones put the app in background mode when the app is not active for some time. I want the app to be running all the time even its in standby mode(standby mode means when we press the home button the app will go to background. and it will run for some time).
I found following code and I tried that
PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
String packageName = "org.traccar.client";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent i = new Intent();
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
}
else{
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
}
}
Even after working with the code the default state is Battery Saver(recommended)
I want the app in No Restriction mode once the app is opened, any solution for this?
The code you use is for battery optimization. Settings-->Batery-->Three Dots Menu Item (...)--->Battery Optimization-->(Choose an app from list)--->Optimize/ Don't optimize.
By choosing Don't optimize you are essentially bypassing Doze, not app standby.
Also be advised that doing this programmatically as you do may result in Google taking your app off the store. It is safer to do it manually following the path i described above.
More on Doze and App Standby 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
Hí,
I have an java application that makes use bluetooth to search for devices. When the user presses the native Bluetooth button to turn it off my application shows the bluetooth status offline. When the user presses the native Bluetooth button to turn it on, my application should go back to work but it does not. For my scan back to work I need to close and open my application.
How to fix this programmatically ?
Make sure you have the following permissions in your manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
You can enable bluetooth by using the following in an Activity or Service class:
BluetoothAdapter.getDefaultAdapter().enable();
If you are trying to listen for when Bluetooth is enabled, register a BroadcastReceiver listening for the BluetoothAdapter.ACTION_STATE_CHANGED action:
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
if (state == BluetoothAdapter.STATE_OFF) {
// Bluetooth is turned off
} else if (state == BluetoothAdapter.STATE_ON) {
// Do your scan
}
I am trying to use UsageStatsManager on an Android Wear device.
What I did:
I am requesting the permission for it on the mobile app that is running on my phone. After the permission was given by the user, I can access the usage stats data.
Then I created a UsageStatsManager on the wear app and tried to access the usage stats data there. However, on the watch, I do not have the permission to do that and I can not ask for permission. To check if the permission was given by the user I used:
private boolean isPermissionGranted(){
try{
PackageManager packageManager = getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
return mode == AppOpsManager.MODE_ALLOWED;
}catch(PackageManager.NameNotFoundException e){
return false;
}
}
This returns true on the phone but false on the watch.
The problem:
I can ask the user for permission to use UsageStatsManager on the phone but I can not do that on the watch because the setting does not exist. Has anyone figured out if it is possible to get UsageStatsManager running on a watch?
Usage Permission intent isn't possible on Wear.
Same goes for Overlay (Settings.canDrawOverlays()) though at least you can use lower target to overcome it.
Also with latest Android N emulator this isn't resolved.
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);