Asus Nexus 7 2013 camera.open Fail to connect to camera service - android

I have an issue only on Asus Nexus 7 2013. All other devices, including Asus Nexus 7 2012 works fine.
This device has 2 camera IDs: 0, 1.
But I can't open camera, using both of them.
//...
try {
//cameraID: 0 or 1
camera = Camera.open(cameraID);
//...
} catch (Exception e) {
e.printStackTrace();
}
So I getting:
java.lang.RuntimeException: Fail to connect to camera service
android.hardware.Camera.<init>(Camera.java:495)
android.hardware.Camera.open(Camera.java:341)

Problem was in new Android 6+ feature: Request Permission at Runtime.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!permissionRequestInProgress) {
permissionRequestInProgress = true;
new Handler().post(new Runnable() {
#Override
public void run() {
if (ContextCompat.checkSelfPermission(RootActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(RootActivity.this,
new String[]{Manifest.permission.CAMERA},
CAMERA_PERMISSION_REQUEST);
} else {
//we got it
}
}
});
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case CAMERA_PERMISSION_REQUEST:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
//we got it
} else {
// permission denied
}
permissionRequestInProgress = false;
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Related

My application running at lollipop version but can't run at higher versions

My application running at lollipop version but can't run at higher versions. This error occurs:
The problem is that in 6.0 or above versions , you need to use runtime permissions.
Follow the sample code below :
public void checkPer()
{
if ((ContextCompat.checkSelfPermission(SubCategoryActivity.this,"android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
} else {
// user already provided permission
// perform function for what you want to achieve
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean canUseExternalStorage = false;
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
canUseExternalStorage = true;
}
if (!canUseExternalStorage) {
Toast.makeText(SubCategoryActivity.this, "You cannot see images without requested permission", Toast.LENGTH_SHORT).show();
} else {
// user now provided permission
// perform function for what you want to achieve
}
}
}
}
This will help you my friend.

Android, can't get permission for Camera dialog box to appear Marshmallow version

I can't seem to get the dialog box to pop up for camera request permission. I don't see what mistake I made. I think I followed the Android Developer document on permissions well enough yet I'm still not seeing a dialog box appear for permissions. What is wrong with the code and what can I do to fix it?
private static final int REQUEST_CAMERA_PERMISSION_RESULT = 0;
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// v checks for request code
if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT){
// v check if request was granted or not
if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(),
"Application will not run without camera services", Toast.LENGTH_SHORT).show();
}
}
}
private void connectCamera() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); //connects to camera
try {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //android version "marshmallow" requires permissions
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED) { //checks if permission is granted or not
cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
} else { //if permission isn't granted. Tells user, app needs permission for camera
if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
Toast.makeText(this,
"app requires access to camera", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[] { Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO
}, REQUEST_CAMERA_PERMISSION_RESULT);
}
} else {
cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

Camera permission is not working on Marshmallow and above devices and crashes

Since I have made a flashlight application and it works properly on the device which have SDK 22 and below. But when I come to check on marshmallow and above devices, it doesn't run and crashes at the beginning only I asked for permission using following code but it doesn't seems to be working at all. here is my code for requesting permission of camera at run time. here is my code:
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 5) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// if permission not granted it will force close the app
}
}
I have tried in marshmallow, nougat devices but it doesnt ask for camera permission and i have to go through manually from setting-app-flashlight-permission-allow. can anyone help me please. Currently I am testing my app in Lineage os 7.1.1
You need to add the SDK 23 permission in manifest. add the below line in manifest then you will get runtime permission callback.
<uses-permission-sdk-23 android:name="android.permission.CAMERA"/>
Just you change only if condition your code is perfect like this,
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
to
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if( ContextCompat.checkSelfPermission(this,
android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
if(currentAPIVersion>= 23)
{
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
Manifest.permission.CAMERA)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Camera Permission Necessary");
alertBuilder.setMessage("Camera permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
return false;
} else {
return true;
}
} else {
return true;
}
Use the below code and call the "requestPermission" method in onCreate of the Activity:
private static final int REQUEST_CODE_PERMISSION = 2;
List<String> mPermission=new ArrayList<String>();
public void requestPermission()
{
try {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= MockPackageManager.PERMISSION_GRANTED)
mPermission.add(Manifest.permission.CAMERA);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= MockPackageManager.PERMISSION_GRANTED
)
mPermission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE
);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= MockPackageManager.PERMISSION_GRANTED)
mPermission.add(Manifest.permission.READ_EXTERNAL_STORAGE);
if(mPermission.size()>0)
{
String[] array = mPermission.toArray(new String[mPermission.size()]);
ActivityCompat.requestPermissions(this, array, REQUEST_CODE_PERMISSION);
// If any permission aboe not allowed by user, this condition will execute every tim, else your else part will work
}
} catch (Exception e) {
e.printStackTrace();
}
}
Overwrite the method "onRequestPermissionsResult":
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.e("Req Code", "" + requestCode);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (grantResults.length == mPermission.size()) {
for(int i=0;i<grantResults.length;i++)
{
if(grantResults[i] == MockPackageManager.PERMISSION_GRANTED)
{
//don't do anything....
}
else{
mPermission=new ArrayList<String>();
requestPermission();
break;
}
}
}
else{
Toast.makeText(this,"permition not granted",Toast.LENGTH_LONG).show();
mPermission=new ArrayList<String>();
requestPermission();
}
}
}
do this way:
if (ContextCompat.checkSelfPermission(UserProfileActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(UserProfileActivity.this, new String[]{Manifest.permission.CAMERA},
5);
} else {
//start flashlight
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
switch (requestCode) {
case 5: {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
//start flashlight
Log.d("Permissions CAMERA", "Permission Granted: " + permissions[i]);
} else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
Log.d("Permissions CAMERA", "Permission Denied: " + permissions[i]);
}
}
break;
}
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
} break;
}
}
I also had the same problem but managed to fixed it. Here's my case (I don't know if its the same as yours but it's worth a try)
So I have this two activities namely: MainActivity, and FlashlightActivity.
I used to ask for camera permission explicitly in FlaslightActivity just before I tap the switch button but I forgot that I was checking if a user's phone has a camera in the onCreate() method. while I was asking for permission onClick of the switch button of the flashlight, in which that was after I checked if a user's phone has a camera. So what I did was to ask for the permission in my MainActivity just before I start FlashlightActivity.
flashBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(hasPermission()){
Intent flashIntent = new Intent("FlashlightActivity");
startActivity(flashIntent);
}else{
requestPermission();
}
}
});
So basically ask for the permission before you call any Camera related stuffs

Runtime Permission in Redmi Phone

I am trying to send sms targeted version 24 with runtime permission. It gives SecurityException in Redmi Mi 3s device but working fine on other device with Marshmallow OS.
final int REQ_CODE = 100;
void requestPermission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
CTLogs.printLogs( "Permission is not granted, requesting");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS,Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS}, REQ_CODE);
// button.setEnabled(false);
} else {
CTLogs.printLogs("Permission has been granted");
sendSMS();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQ_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
CTLogs.printLogs("Permission has been granted");
sendSMS();
} else {
CTLogs.printLogs("Permission denied !!!");
}
}
}
Try this:
// The permission required by the application to work properly
protected static final String[] requiredPermissions;
private static final int PERMISSION_REQUEST = 0;
static {
List<String> perms = new ArrayList<>(Arrays.asList(
Manifest.permission.RECEIVE_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.SEND_SMS
));
requiredPermissions = perms.toArray(new String[perms.size()]);
}
Call verifyPermissions() method in onCreate():
private void verifyPermissions() {
if (!hasAllPermissions()) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
requiredPermissions,
PERMISSION_REQUEST
);
}
}
private boolean hasAllPermissions() {
// Check if we have all required permissions.
for (String perm : requiredPermissions) {
if (ActivityCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (!hasAllPermissions()) {
finish();
}
return;
}
}
}
once a uninstall app then put this and then install it.
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}

Run time Permission popup click issue in Marshmallow Updated devices

I integrated Android Runtime Permission for READ PHONE STATE for Marshmallow devices. This implementation is working fine and the popup is showing in my application with allow/deny option.
I can click the allow/ deny button for normal marshmallow devices. But in the case of updated android devices(From Lollipop to Marshmallow), the allow button click is not working initial time. This issue is tested and reproduced in Nexus 5 and Nexus 7. Is anything we need to create additional for updated OS? Or is it a Marshmallow issue?
Please check the complete code:
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 101;
if (Build.VERSION.SDK_INT >= 23) {
if(mActivity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) !=PackageManager.PERMISSION_GRANTED) {
if (this.shouldShowRequestPermissionRationale(
Manifest.permission.READ_PHONE_STATE)) {
showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
} else {
this.requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
} else {
handleLoginAPI();
}
} else {
handleLoginAPI();
}
private void handleLoginAPI() {
if (super.isNetworkConnectionAvailable(mActivity)) {
// Api Call from here..
}else{
// No Nw Connection.
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
handleLoginAPI();
} else if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_DENIED) {
if (this.shouldShowRequestPermissionRationale(
Manifest.permission.READ_PHONE_STATE)) {
showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
}
}
return;
}
}
}
Don't call shouldShowRequestPermissionRationale() before requesting the permission. That call is intended to be made after the user has rejected the request, usually in the onRequestPermissionsResult() method. If your app needs to explain why it needs a permission before it asks for it, the app's internal logic needs to decide that.
private void setCheck()
{
int hasWriteContactsPermission = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
hasWriteContactsPermission = checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
if (!shouldShowRequestPermissionRationale(android.Manifest.permission.READ_PHONE_STATE)) {
showLocationRationleDalog();
return;
}
return;
} else {
handleLoginAPI();
}
} else {
handleLoginAPI();
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length == 1
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
handleLoginAPI();
} else {
Log.i("Permission denied");
}
return;
}
}
}
This is an Android OS update issue. The issue is reported in Nexus device. https://code.google.com/p/android/issues/detail?id=213120. The issue can be reproducible in other android native application as like Google map, Gmail etc.

Categories

Resources