权限请求对话框未显示

时间:2017-06-10 17:33:00

标签: android android-permissions android-external-storage

我正在创建一个应用程序来捕获图像,然后将其保存在SD卡中并将其显示在应用程序的MainActivity的 ImageView(image_view)上(在onRequestPermissionsResult()方法)。该应用程序还有助于发送电子邮件用户点击的图片。为此,我已经要求2次访问:

1。)相机请求

2.。)外部存储请求

当应用程序启动时,将显示“摄像机请求”对话框,但未显示“外部存储请求”对话框,并且应用程序崩溃显示“吐司”,因为尚未授予外部写入权限。无法保存文件

我在Manifest中也提到过请求

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

请求权限的代码

public void requestPermissions() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_EXTERNAL_STORAGE);
    }

    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        launchCamera();
    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.CAMERA)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.CAMERA},
                REQUEST_CAMERA);
    }
}

以下是完整代码

public class MainActivity extends AppCompatActivity {

Button clickPhotoButton;
private static final String FILE_NAME="image01.jpg";
private static final int CAMERA_PIC_REQUEST = 100;
private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
private static final int REQUEST_CAMERA = 2;
File pictureDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Prakhar");
Uri fileUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    clickPhotoButton = (Button) findViewById(R.id.click_photo_button);

    makeFolder();
}

public void makeFolder(){

    if(!pictureDir.exists()) {
        pictureDir.mkdirs();
    }
    Log.e("Prakhar", pictureDir.getAbsolutePath());
}

public void requestPermissions() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_EXTERNAL_STORAGE);
    }

    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        launchCamera();
    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.CAMERA)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.CAMERA},
                REQUEST_CAMERA);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if(requestCode == REQUEST_WRITE_EXTERNAL_STORAGE ) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            launchCamera();
        } else {
            Toast.makeText(this,
                    "External write permission has not been granted, cannot saved images",
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    if(requestCode == REQUEST_CAMERA) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            launchCamera();
        } else {
            Toast.makeText(this,
                    "External write permission has not been granted, cannot saved images",
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

public void onPhotoClicked(View view) {

    requestPermissions();
}

public void launchCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File image = new File(pictureDir, FILE_NAME);
    fileUri = Uri.fromFile(image);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA_PIC_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
        ImageView imageView = (ImageView) findViewById(R.id.image_view);
        File image = new File(pictureDir, FILE_NAME);
        fileUri = Uri.fromFile(image);
        imageView.setImageURI(fileUri);
    }
}
}

2 个答案:

答案 0 :(得分:1)

找到它。这真是浪费时间。在我写的清单文件中

android.permissions.WRITE_EXTERNAL_STORAGE

然而它应该是

android.permission.WRITE_EXTERNAL_STORAGE

答案 1 :(得分:0)

试试这个

 public void requestPermissions() {
    boolean hasStoragePermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

    boolean hasCameraPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;

    ArrayList<String> permissonList = new ArrayList();

    if (!hasCameraPermission) {
        permissonList.add(android.Manifest.permission.CAMERA)
    }

    if (!hasStoragePermission) {
        permissonList.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }

    if (permissonList.size() > 0) {
        String [] permissionArray = permissonList.toArray(new String[]{});

        ActivityCompat.requestPermissions(this,permissionArray,
                REQUEST_PERMISSION);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {

    if (requestCode == REQUEST_PERMISSION) {
        boolean allPermissionGranted = true;

        for (int i =0; i < grantResults.length; i++) {
            boolean permissionGranted = grantResults[i] == PackageManager.PERMISSION_GRANTED;

            if (!permisssionGranted) {
                allPermissionGranted = false;
                break;
            }
        }


        if (allPermissionGranted) {
            launchCamera();
        }
    }
}