1

I am developing an application for Android Marshmallow (API LEvel 23).

I have an Activity which contains a ListView.
I populate that ListView by using a BaseView Adapter.

I want to start an Activity whenever I push a button inside my ListView.
However, this Activity needs to access the camera which in the latest Android version means that I should ask for the camera permission during runtime.
The camera using Activity will be an ZXing Activity.

Here is the Activity code:

public class ProviderListActivity extends AppCompatActivity {

    public boolean can_access_camera = false;
    public final int got_camera = PackageManager.PERMISSION_DENIED;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider_list);
        ListView listView = (ListView) findViewById(R.id.ProviderListView);
        CardsDBHelper dbHelper = new CardsDBHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        listView.setAdapter(new ProviderListAdapter(this, db));
    }

    public void start_scan(){
        if (have_camera_permission()){
            start_scan_activity();
        } else {
            Log.d("ProviderListActivity", "I CANNOT ACCESS THE CAMERA");
        }
    }

    public void start_scan_activity(){
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.initiateScan();
    }

    public boolean have_camera_permission(){
        //Check for permissions
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (permissionCheck == PackageManager.PERMISSION_DENIED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, got_camera);
        } else {
            can_access_camera = true;
        }
        return can_access_camera;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case got_camera: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    can_access_camera = true;
                } else {
                    can_access_camera = false;
                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
}

The getView() method in my Adapter is:

public View getView(int position, View convertView, final ViewGroup parent) {
Button grid_button;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        grid_button = new Button(mContext);
        grid_button.setText("Hello Text");
        Drawable d = mContext.getDrawable(R.drawable.circle);
    grid_button.setCompoundDrawablesRelativeWithIntrinsicBounds(null,d,null,null);
    grid_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //What do I need to put inside here?
        }
    });
} else {
    grid_button = (Button) convertView;
}

return grid_button;
}

You may have noticed that the Adapter gets an extra db argument.
This is a SQLite database object which is which is used inside the Adapter, but this is not relevant.

I am not sure how to call my scan Activity by using the onClick override.
I tried passing the parent Activity as an argument in the Adapter and call the scan method, but I get the following exception:

java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode` 

Does anyone have a solution for my problem?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
msmechanized
  • 436
  • 4
  • 19
  • For **java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode** Error, Use requestCode between **0 to 255**. This error occur if you use Request code more than 255. – Dhaval Patel Nov 16 '15 at 18:52
  • Hello. I cannot really do that because I use the following line `ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, got_camera);` and the `Manifest.permission.CAMERA` is defined in the android API – msmechanized Nov 16 '15 at 18:56
  • Set **got_camera** value between 1 to 255 not more than 255. like **public final int got_camera = 24;** – Dhaval Patel Nov 16 '15 at 18:58
  • Correction to first comment it's 1 to 255 not 0 to 255. – Dhaval Patel Nov 16 '15 at 19:00
  • You are getting error because **public final int got_camera = PackageManager.PERMISSION_DENIED;** will set **got_camera =-1** and -1 is not acceptable request code for requestPermissions method, hence java.lang.IllegalArgumentException. – Dhaval Patel Nov 16 '15 at 19:01
  • @DhavalPatel Thanks! This solved it for me. But Why 24? Isn't this a bit arbitrary? – msmechanized Nov 16 '15 at 19:05
  • you can use any number between 1 to 255. I have used 24 because it's my favorite. ;-p – Dhaval Patel Nov 16 '15 at 19:05
  • Try this it may be work http://stackoverflow.com/a/41221852/5488468 – Bipin Bharti Jan 03 '17 at 11:06

1 Answers1

4

You are getting java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode Error because you are using -1(PackageManager.PERMISSION_DENIED) as requestCode for requestPermissions method.

To make it work use any requestCode between 1 to 255.

public final int got_camera = 24;
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46