I'm using the zxing library to provide qrcodes and scanning to an application however I'm stuck on an issue. When attempting to scan, I've tried several methods. The recommended:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
or the Intent method:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
this.startActivityForResult(intent, 0);
Both appear to work fine as the Scan app or choose the app to handle the intent dialog appears. However, after scanning, the app immediately closes. No errors and no results. Just bye-bye.
method to catch the result, note the Log.d immediately after super() is never displayed.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(LOG_TAG, "resultcode = " + resultCode);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, data);
if (scanResult == null) {
return;
}
final String result = scanResult.getContents(); // Your result
if (result != null) {
Log.d(LOG_TAG, "Your result is: " + result);
}
break;
default:
}
}
I've seen references in some tutorials explaining how to implement zxing make entries in the AndroidManifest.xml. Seen below:
<activity android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
I'm just not having any luck so far and the lack of logs isn't helping point me in any direction.
I'm using Android Studio and have gradle setup for the libs in dependencies:
compile files('libs/core-3.1.1.jar')
compile files('libs/android-integration-3.1.1.jar')
Help is really appreciated.
The problem is not with zxing, but rather with the way you start it. The second parameter of the startActivityForResult
method is a requestCode
(documentation). You would then check that requestCode
on onActivityResult
to handle the response. Also, do not call super
first in onActivityResult
.
For example:
startActivityForResult(intent, 123);
will cause Android to call onActivityResult with request code 123
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 123 && resultCode == RESULT_OK) {
//do some stuff...
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}