我使用zxing读取QR码。当我在手机处于纵向模式时扫描代码时它工作正常,但是当我在横向模式下扫描它似乎返回时,但onActivityResult
方法从未被调用(我的断点甚至没有被击中)
来自构建文件:
compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'
活动代码。这被调用,但此时fragment
为空:
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (fragment != null && Activity.RESULT_OK == resultCode) {
fragment.onActivityResult(requestCode, resultCode, intent);
}
}
以下是片段中的代码:
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
// Handle QR scan result
if (Activity.RESULT_OK == resultCode) {
final IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
final String contentString = scanResult.getContents();
if (Strings.isNotEmpty(contentString)) {
processDataAsynchronously(contentString);
}
}
}
}
OnClick代码:
@Override
public void onClick(final View v) {
int viewId = v.getId();
if (viewId == R.id.check_qr_button) {
// Get permissions to use the camera
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
final Runnable cameraPermissionsRunnable = new Runnable() {
@Override
public void run() {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
};
// Why we request this permission
DialogManager.showOkDialog(getActivity(), R.string.permissions_explanation_camera_qr, true, cameraPermissionsRunnable, true, true);
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
} else {
IntentIntegrator.forSupportFragment(this).initiateScan();
//ALSO TRIED THIS BUT DIDN'T WORK EITHER
/*
final IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.setOrientationLocked(false);
integrator.setBeepEnabled(true);
integrator.initiateScan();
*/
}
return;
}
super.onClick(v);
}
感谢您的任何提示!