I'm developing an Android app. That app has the functionality to read QR codes(That's the only capability that has been implemented to it so far). I'm developing the app on Android Studio. Once I install the app on to my android phone, it says permission denied. (Requesting permission has been implemented for the app and the "Permission denied" is the custom message that I've put into the android app in cases where the permission gets denied.).
I can manually give permission to the app on my phone. When I do that, it says permission granted and for a second gets access to the Camera but then "Permission Denied" message starts to appear repetitively and the camera feed get's lost.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.folio" >
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.folio;
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
//import sun.applet.Main;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA=1;
private ZXingScannerView scannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView=new ZXingScannerView(this);
setContentView(scannerView);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(checkPermission())
{
Toast.makeText(MainActivity.this, "Permission granted!", Toast.LENGTH_LONG).show();
}
else
{
requestPermission();
}
}
}
private boolean checkPermission()
{
return (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED);
}
private void requestPermission()
{
ActivityCompat.requestPermissions(this, new String[]{CAMERA_SERVICE}, REQUEST_CAMERA);
}
public void onRequestPermissionsResult(int requestCode, String permission[], int grantResults[])
{
switch(requestCode)
{
case REQUEST_CAMERA:
if(grantResults.length>0)
{
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if(cameraAccepted)
{
Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Permission Denied!", Toast.LENGTH_LONG).show();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(shouldShowRequestPermissionRationale(CAMERA_SERVICE))
{
displayAlertMessage("Please allow access!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{CAMERA_SERVICE}, REQUEST_CAMERA);
}
});
return;
}
}
}
}
break;
}
}
@Override
public void onResume()
{
super.onResume();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(checkPermission())
{
if(scannerView==null)
{
scannerView=new ZXingScannerView(this);
setContentView(scannerView);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
}
{
requestPermission();
}
}
}
@Override
public void onDestroy()
{
super.onDestroy();
scannerView.stopCamera();
}
public void displayAlertMessage(String message, DialogInterface.OnClickListener listener)
{
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("Ok", listener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void handleResult(Result result) {
final String scanResult=result.getText();
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Scan result");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scannerView.resumeCameraPreview(MainActivity.this);
}
});
builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(scanResult));
startActivity(intent);
}
});
builder.setMessage(scanResult);
AlertDialog alert=builder.create();
alert.show();
}
}
``````````````````````````````````````````````````````````````````````
ActivityCompat.requestPermissions(this, new String[]{CAMERA_SERVICE}, REQUEST_CAMERA);
It is unclear what CAMERA_SERVICE
is. However, this is supposed to be an array of permissions, so most likely you should be using Manifest.permission.CAMERA
here:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);