I want to use ZXingScannerView
, to read QR-code with my app.
I need to grant camera permission to use ZXingScannerView
, right?
The view doesn't work for some reason which I don't know.
Anyway, I thought so, so I tried to grant permission, but it keeps failing, saying "Package installer has stopped".
I think the bug is occur on line
if(permissioncheck == PackageManager.PERMISSION_GRANTED)
Here is entire code
ZXingScannerView mScanview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int permissioncheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if(permissioncheck == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "not ok", Toast.LENGTH_SHORT).show();
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
Toast.makeText(this, "ok?", Toast.LENGTH_SHORT).show();
}
else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, 1);
}
}
Button button = (Button)findViewById(R.id.qr_button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
mScanview = new ZXingScannerView(getApplicationContext());
setContentView(mScanview);
}
});
}
@Override
public void onRequestPermissionsResult(int reqCode, String permissions[], int[] grantResult){
switch (reqCode){
case 1: {
if(grantResult.length > 0 && grantResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "not ok", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
@Override
public void handleResult(Result result){
Log.v("TAG", result.getText());
Log.v("TAG", result.getBarcodeFormat().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog dialog = builder.create();
dialog.show();
}
@Override
public void onPause() {
super.onPause();
mScanview.stopCamera();
}
Or, if you know better way of implementing QR-code reader in app, Could you let me know about that?