0

バーコード スキャナー アプリにカメラを使用しており、一部のデバイス (LG G Flex、Asus Nexus 7) で Android ランタイム例外 - カメラ サービスに接続できませんでした。以下のメニフェストファイルのスニペットは次のとおりです。

`uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.CAMERA"
....`

一時停止、停止、および破棄時にカメラを解放しています。

    /** 
* Restarts the camera. 
*/ 
@Override 
protected void onResume() { 
super.onResume(); 
try { 
startCameraSource(); 
} catch (Exception e) { 
e.printStackTrace();

    }
}

/**
 * Stops the camera.
 */
@Override
protected void onPause() {
    super.onPause();
    if (mPreview != null) {
        mPreview.stop();
    }
}

/**
 * Releases the resources associated with the camera source, the associated detectors, and the
 * rest of the processing pipeline.
 */
@Override
protected void onDestroy() {
    super.onDestroy();
    if (mPreview != null) {
        mPreview.release();
    }

}

まだランタイム例外を超えていますが、再現できるように2つ以上のデバイスがありません。その問題の解決策はありますか?

4

1 に答える 1

0

こんにちは、menifest ファイルに次の権限を追加する必要があります

<uses-permission android:name="android.permission.CAMERA" />

Android M パーミッション モデルを使用している場合は、最初に実行時にアプリにこのパーミッションがあるかどうかを確認し、実行時にこのパーミッションをユーザーに要求する必要があります。マニフェストで定義したアクセス許可は、インストール時に自動的に付与されるわけではありません。

 if (checkSelfPermission(Manifest.permission.CAMERA)
    != PackageManager.PERMISSION_GRANTED) {

requestPermissions(new String[]{Manifest.permission.CAMERA},
        MY_REQUEST_CODE);
}

ダイアログ結果のコールバックが必要になります。

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions,      int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Now user should be able to use camera
    }
    else {
        // Your app will not have this permission. Turn off all functions 
        // that require this permission or it will force close like your 
        // original question
    }
}
}
于 2016-10-17T09:16:12.157 に答える