Android 在棒棒糖中打开相机时出现空白屏幕

Android 在棒棒糖中打开相机时出现空白屏幕,android,android-camera,android-5.0-lollipop,android-5.1.1-lollipop,Android,Android Camera,Android 5.0 Lollipop,Android 5.1.1 Lollipop,我使用的是Zxing条形码扫描仪,我的应用程序运行良好,除了棒棒糖,我有一个问题: 当我打开相机进行扫描时,我会看到一个空白屏幕,并干杯以确保我得到了许可,但仍然无法工作 我在检查操作系统构建版本是否为棒棒糖时做了一些改变,但我仍然有同样的问题 这是我的密码: public class QrCodeScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{ private st

我使用的是Zxing条形码扫描仪,我的应用程序运行良好,除了棒棒糖,我有一个问题:

当我打开相机进行扫描时,我会看到一个空白屏幕,并干杯以确保我得到了许可,但仍然无法工作

我在检查操作系统构建版本是否为棒棒糖时做了一些改变,但我仍然有同样的问题

这是我的密码:

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>


谢谢你的回答

检查您的恢复功能


存在一个
if
条件,即摄像头仅对安卓M及以上版本进行初始化。需要添加
else
条件并初始化相机。

您的手动测试中有这个吗?不是权限,而是功能?从你的代码中我可以看到2个授予权限的祝酒词。你能告诉我调用的是哪一个吗?“已授予权限,现在您可以访问摄像头”“已授予权限”我现在添加了
,但仍然不工作,我的权限是
,您可以将完整的Manifest.xml文件添加到问题中吗?完成后,您可以检查它