我正在尝试在我在Android Studio中设置的实际连接的设备Alps M16(Android 6.0,API 23)中启动play-visions-vision的条形码扫描仪。但是每次尝试启动条形码扫描仪时,当相机启动条形码扫描仪时,都会弹出此错误:
E / DynamiteModule:无法加载DynamiteLoader: java.lang.NullPointerException:尝试调用虚拟方法 布尔值java.lang.String.contains(java.lang.CharSequence) 对象引用
W / DynamiteModule:无法加载远程模块: 无法获取模块上下文
V / ProviderLeakDetecter:游标 关闭:180333163打开的光标总数:0。
E / BarcodeNativeHandle: 错误加载模块
com.google.android.gms.dynamite.DynamiteModule $ LoadingException: 远程加载失败。找不到本地备用。
在较旧的设备上不会发生此错误,只会在特定设备上发生。中兴K85(Android 5.0.2,API 21)没有错误。
我使用的库是com.google.android.gms:play-services-vision:15.0.2
启动相机的活动:
package io.cordova.hellocordova;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;
public class ScanBarcodeActivity extends Activity {
SurfaceView cameraPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_scan_barcode);
cameraPreview = (SurfaceView) findViewById(R.id.camera_preview);
createCameraSource();
}
private void createCameraSource() {
BarcodeDetector detector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
CameraSource cs = new CameraSource.Builder(this, detector)
.setAutoFocusEnabled(true)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedPreviewSize(1600, 1024)
.build();
cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (ActivityCompat.checkSelfPermission(ScanBarcodeActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
String[] arr = { Manifest.permission.CAMERA };
// TODO: Consider calling
ActivityCompat.requestPermissions(ScanBarcodeActivity.this, arr , 2);
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
try {
cs.start(cameraPreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cs.stop();
}
});
Detector.Processor<Barcode> pb = new Detector.Processor<Barcode>() {
@Override
public void release() {
System.out.println("ended");
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() > 0) {
Intent intent = new Intent();
intent.putExtra("barcode", barcodes.valueAt(0).displayValue);
setResult(CommonStatusCodes.SUCCESS, intent);
finish();
}
}
};
detector.setProcessor(pb);
if (!detector.isOperational()) {
System.out.println("Could not set up the detector!");
return;
}
}
}