I tried to access the camera from the phone, it worked 3 times, after the screen went black (Print below) in API 24, I tested it on a cell phone with API 22 and it worked, with the API 24 no, does anyone know how to solve it?
public class fotos extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView ivPhoto;
private Button btTakeaaPhoto;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fotos);
ivPhoto = findViewById(R.id.ivPhoto);
btTakeaaPhoto = findViewById(R.id.btTakeaPhoto);;
btTakeaaPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//config.showProgress(true, progressBar, context);
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ivPhoto.setImageBitmap(imageBitmap);
}
}
}
The screen stays like this for a few seconds and then the camera closes and returns to the activity.
Edit: The application does not stop working, it just opens the camera screen, turns black, and after a few seconds it closes, as I already said
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
The problem is not the permissions, they have already been given, I already looked at the configurations to confirm. That's not the problem.
You might want to check several parts in your project.
1. in you AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
check if you've already add such permission requests.
2. in your source code, for permission request from Android 6. You should have following code.
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
return;
}
for requestCameraPermission() function
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
new ConfirmationDialog().show(getChildFragmentManager(), FRAGMENT_DIALOG);
} else {
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
you should ask that user to grant the permission and implement the callback function
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length != 1 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
ErrorDialog.newInstance(getString(R.string.request_permission))
.show(getChildFragmentManager(), FRAGMENT_DIALOG);
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
After you granted the permission, open your camera and config the correct parameters. you can check the repo here https://github.com/wangchauyan/camera_sample.git, which I used to create my own camera application.
Hope that would be helpful.
I did it work, and do you know how? I rebooted the cell phone, do you know how I discovered it? I tried using the camera's function of WhastApp and showed me the message that it was not possible to access the camera at that moment, which was for me to reboot, so after that, it magically worked. WWWTTFFFF
Related
I am trying to request WRITE_EXTERNAL_STORAGE permission on Android 10. I do that using the following code:
ArrayList<String> permissions = new ArrayList<>();
if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (this.checkSelfPermission(ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
this.checkSelfPermission(ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}
if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissions.size() > 0) requestPermissions(permissions.toArray(new String[0]), PERMISSION_REQUEST_FINE_LOCATION);
Here is the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
All other permissions work except WRITE_EXTERNAL_STORAGE. When I try to request it, it does not display any dialog for confirming permission, but it simply denies it. Why and how can I fix it?
I have discovered the solution, however the cause of the problem is still a mystery to me. If anyone has an explination, it would be greatly appreciated. Basically my issue is a duplicate of this issue. All I had to do is to change the following line from
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
to
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>
What does tools:node="replace" do?
You can handle requested permission using this method (works with android 10), Here is the documentation.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch (requestCode) {
case PERMISSION_REQUEST_ID_STORAGE:
// permisstion granted by user
break;
}
} else {
switch (requestCode) {
case PERMISSION_REQUEST_ID_STORAGE:
// permission denied by user
break;
}
}
}
You can use Android Runtime Permission Library by nabinbhandari
in you build.gradle app module file write this
//Android Runtime Permission Library
implementation 'com.nabinbhandari.android:permissions:3.8'
In your Manifest file write
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and in Activity declare these variables
String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
String rationale = "Please provide storage permission to proceed ...";
And in your onCreate() Method write this
Permissions.Options options = new Permissions.Options()
.setRationaleDialogTitle("Info")
.setSettingsDialogTitle("Warning");
Permissions.check(MainActivity.this, permissions, rationale, options, new PermissionHandler() {
#Override
public void onGranted() {
// do your task.
Toast.makeText(MainActivity.this, "Permissions granted", Toast.LENGTH_SHORT).show();
}
#Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied
Toast.makeText(MainActivity.this, "Permissions denied", Toast.LENGTH_SHORT).show();
}
});
I have a android app in play store. That has only internet permission. Now I change in app user can use their camera & storage.
I add privacy policy on play store and in link in app also. I also mention these permission in release text.
When I upload new app display message like this
minSdkVersion 21
targetSdkVersion 28
versionCode 4
versionName "4.0"
Permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera2.full" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Permission Code
private void permission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(StartActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
//Display_Rermission_Reason();
}else {
Intent intent = new Intent(StartActivity.this, PrivacyPolicyActivity.class);
startActivity(intent);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// close the app
Toast.makeText(MainActivity.this, "Sorry!!!, you can't use this app without granting permission", Toast.LENGTH_LONG).show();
//Display_Rermission_Reason();
}
}
}
After publish the app it display in google play store
This app is incompatible with all of your devices.
Any version 5+ and above
Change this in manifest,
<uses-feature android:name="android.hardware.camera2.full"
android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="false" />
Have a look at this link below which might help you,
https://developer.android.com/guide/topics/manifest/uses-feature-element
I add bluetooth to my app but am running into the following problem. When I do the code:
BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
}
if(!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
The error is on the following line:
if(!bluetoothAdapter.isEnabled())
Error:
Missing permissions required by BluetoothAdapter.isEnabled :android.permissions.BLUETOOTH
I added the following permissions to the android manifest file:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
but am still getting the same issue. Do you know what I did wrong?
You only need <uses-permission> tag since you are using a permission. You don't need <permissions> tag since this is not your custom permission.
Maybe you have placed <uses-permission> tag in the wrong element in manifest, but I cannot say anything else with the information you provided
Since your bluetoothAdapter can be null, either add to second IF null pointer check, or add a RETURN in first IF
It is also nice to have <uses-feature> tag
Let's say you have a button and then you click on it, Bluetooth needs to turn on. Here's a quick example how you would do it for Android 6.0 and above.
First, declare this variable in your Activity/Fragment:
public static final int PERMISSION_ASK = 1001;
Now, let's say this buttons is going to enable the bluetooth.
setOnClickListener:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(isBluetoothPermissionGranted()) {
// app already has required permissions
// do some task here
} else {
// app does not have permission yet.
// ask for permissions
askForBluetoothPermissions();
}
} else {
// Android version below 6.0, no need to check or ask for permission
// do some task here
}
}
});
Checking if application already has the required permission:
#RequiresApi(api = Build.VERSION_CODES.M)
private boolean isBluetoothPermissionGranted() {
boolean granted = false;
int bluetoothGranted = checkSelfPermission(Manifest.permission.BLUETOOTH);
int bluetoothAdminGranted = checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN);
if(bluetoothGranted == PackageManager.PERMISSION_GRANTED &&
bluetoothAdminGranted == PackageManager.PERMISSION_GRANTED) {
granted = true;
}
return granted;
}
If required permission is not granted, here's how you would ask for it:
#RequiresApi(api = Build.VERSION_CODES.M)
private void askForBluetoothPermissions() {
String[] permissions = new String[] {
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN
};
requestPermissions(permissions, PERMISSION_ASK);
}
And finally, here's how you would confirm if the user granted you the permission(s) or not:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_ASK:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// all requested permissions were granted
// perform your task here
} else {
// permissions not granted
// DO NOT PERFORM THE TASK, it will fail/crash
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
Hope this helps
For Android 6.0 and above you must add the following to manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
App crashes after running the program with failed to initialize
Vuforia with permission exception
Android version is <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
testing on device 4.1.1 (api level 16) with front camera only.
Permission included in manifest file:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:glEsVersion="0x00020000" />
exception at SampleApplicationSession's InitVuforiaTask Task, value of Vuforia.init() returned is -1.
Not sure what I missed.
Library included are armaebi-v7a/libVuforia.so, android-support-v4, jpct_ae, Vuforia
I have faced the same problem. If you see the example comes with the compiledSdKversion 22 because in newer versions the user have to explicitly give the Camera permission. My project is working with API 25 by adding some code to my android application. In my case, I asked for the Camera Permission before opening the vuforia activity when an user clic a FloatingActionButton:
FloatingActionButton flb=(FloatingActionButton)findViewById(R.id.floatingActionButton2);
flb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
else
{
Intent myIntent = new Intent(MainActivity.this, VideoPlayback.class);
startActivity(myIntent);
}
}
});
The VideoPlayback is the activity that use AR from vuforia included in the advance examples. In this case you have to listen to an onRequestPermissionsResult because we have to check the user's answer.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
// Begin monitoring for Aruba Beacon-based Campaign events
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Intent myIntent = new Intent(MainActivity.this, VideoPlayback.class);
startActivity(myIntent);
}
}
}
In the onRequestPermissionsResult we check if the answer was positive an if so we open the activity.
I hope it works for you too.
I tried to make an app than can switch my camera flash on and off.
The code I have atm looks like this:
Camera flash;
Camera.Parameters params;
flash = Camera.open();
params = flash.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
flash.setParameters(params);
And in the manifest xml:
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
<permission android:name="android.permission.CAMERA"> </permission>
Everytime I run the code, the app crashes at "flash = Camera.open();" with this error:
java.lang.RuntimeException: Fail to Connect to camera service
What am I doing wrong?
To access the device camera, you must declare the CAMERA permission in your Android Manifest like this,
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
You might have forgotten to call release in onDestroy
For example:
#Override
protected void onDestroy() {
if (mCamera != null) {
mCamera.release();
}
super.onDestroy();
}
Usually that problem is due to the missing camera request permission, as already said by other users.
But, just to register here another cause, if you try to open the camera using a cameraID that does not exist, you will receive that same error
java.lang.RuntimeException: Fail to Connect to camera service
You need to add the new request permission on android 6.x programmatically before.
private static final int MY_PERMISSIONS_REQUEST_CAMERA = 555;
if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
} else {
IntentIntegrator.forSupportFragment(this).initiateScan();
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
IntentIntegrator.forSupportFragment(this).initiateScan();
} else {
FragmentHelper.popFragment(getActivity(), null, null);
}
}
}
}