2
I need to ask for runtime permission to use camera in my fragment class.my code is like this..


int currentapiVersion = android.os.Build.VERSION.SDK_INT;

if (currentapiVersion >= Build.VERSION_CODES.M) {

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

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

しかし、シンボル「CAMERA」を解決できません

4

2 に答える 2

10

以下のステートメントをコードにインポートします。

import android.Manifest;

  int permissionCheckStorage = ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.CAMERA);
                if (permissionCheckStorage == PackageManager.PERMISSION_DENIED) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
                }
于 2016-04-01T06:14:04.483 に答える
1

これを試して:

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-04-01T06:17:13.853 に答える