我正在使用Zxing barcodescanner,我的应用程序运行正常,除了Lollipop我有一个问题:
当我打开相机进行扫描时,我会看到空白屏幕,并举杯祝酒,以确保我获得了许可,但仍无效。
我在检查操作系统构建版本到Lollipop时有所改变,但我仍有同样的问题
这是我的代码:
public class QrCodeScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView mScannerView;
private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
int number = 0;
String IMEI1="null";
String IMEI2="null";
String id_vendeur;
String id="null";
String username="null";
String points_vendeur="null";
String etat="non";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
id_vendeur = intent.getStringExtra("id_vendeur");
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
/******************* SP *********************/
final SharedPreferences sharedpreferences = PreferenceManager
.getDefaultSharedPreferences(this);
if (sharedpreferences.getString("etat","").equals("oui")) {
id = sharedpreferences.getString("id", null);
username = sharedpreferences.getString("user", null);
points_vendeur = sharedpreferences.getString("points_vendeur", null);
etat = sharedpreferences.getString("etat", null);
}
}
private boolean checkPermission() {
return ( ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA ) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA:
if (grantResults.length > 0) {
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted){
Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(CAMERA)) {
showMessageOKCancel("You need to allow access to both the permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{CAMERA},
REQUEST_CAMERA);
}
}
});
return;
}
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new android.support.v7.app.AlertDialog.Builder(QrCodeScannerActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void onResume() {
super.onResume();
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
if(mScannerView == null) {
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}
mScannerView.setResultHandler(this);
mScannerView.startCamera(camId);
} else {
requestPermission();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
mScannerView.stopCamera();
mScannerView = null;
}
@Override
public void handleResult(Result rawResult) {
final String result = rawResult.getText();
/* Log.e("QRCodeScanner", rawResult.getText());
Log.e("QRCodeScanner", rawResult.getBarcodeFormat().toString()); */
final AlertDialog.Builder builder = new AlertDialog.Builder(QrCodeScannerActivity.this)
.setTitle("SCAN IMEI ")
.setCancelable(true)
.setMessage(result)
.setPositiveButton("Valider", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(number+1 == 2) {
IMEI2=result;
number++;
dialog.cancel();
Log.e("Number", " " + number);
Log.e("IMEI : ", result);
Intent intent = new Intent(QrCodeScannerActivity.this, TraitementActivity.class);
intent.putExtra("IMEI1", IMEI1);
intent.putExtra("IMEI2", IMEI2);
intent.putExtra("id_vendeur",id_vendeur);
startActivity(intent);
finish();
}
else{
IMEI1=result;
number++;
dialog.cancel();
Log.e("Number", " " + number);
Log.e("IMEI : ", result);
mScannerView.resumeCameraPreview(QrCodeScannerActivity.this);
}
}
})
.setNeutralButton("Réessayer", new DialogInterface.OnClickListener() { // define the 'Cancel' button
public void onClick(DialogInterface dialog, int which) {
number = 0;
dialog.cancel();
mScannerView.resumeCameraPreview(QrCodeScannerActivity.this);
}
});
builder.show();
}
}
另外,这是我的gradle配置:
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.bomarecompany.streamsystem.fidcom"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
最后我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bomarecompany.streamsystem.fidcom">
<uses-feature android:name="android.hardware.camera" />
<!-- Permissions -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.Dark">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".SignupActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".QrCodeScannerActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".TraitementActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".ProduitActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".NavDrawerActivity"
android:theme="@style/AppTheme" />
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<activity
android:name=".CadeauxActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".ProfileActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".ContactActivity"
android:theme="@style/AppTheme.Dark" />
<activity
android:name=".ProposActivity"
android:theme="@style/AppTheme.Dark" />
<activity android:name=".StatistiqueActivity" />
<activity android:name=".NotificationActivity"
android:theme="@style/DakAB"
android:label="Historique Notifications"></activity>
</application>
</manifest>
感谢您的回答!
答案 0 :(得分:1)
检查您的onResume
功能
有一个if
条件,即相机仅针对android M及更高版本进行初始化。需要添加else
条件并初始化摄像头。