Android:如何在android nougat中编写相机意图

在我的 Android应用程序中,我必须使用相机按钮click.It工作完美的所有Android版本除了安卓7(牛轧糖).当我点击相机选项,即使权限打开,应用程序即将退出.我认为问题出在相机调用intent.Below是我的代码.
camera = (ImageView) dialog.findViewById(R.id.camera);

camera.setonClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        clickCamera();
        dialog.dismiss();
    }
});

private void clickCamera() { // 1 for icon and 2 for attachment
    if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},MY_REQUEST_CODE);
    }else {
        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_REQUEST_CODE_STORAGE);
        }else{
            currentimageUri = getimageFileUri();
            Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intentPicture.putExtra(MediaStore.EXTRA_OUTPUT,currentimageUri); // set the image file name
            // start the image capture Intent
            startActivityForResult(intentPicture,REQUEST_CAMERA);  // 1 for REQUEST_CAMERA and 2 for REQUEST_CAMERA_ATT
        }
    }
}

private static Uri getimageFileUri(){
    // Create a storage directory for the images
    // To be safe(er),you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this

    imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyProject");
    if (! imagePath.exists()){
        if (! imagePath.mkdirs()){
            return null;
        }else{
            //create new folder
        }
    }

    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File image = new File(imagePath,"MyProject_"+ timeStamp + ".jpg");

    if(!image.exists()){
        try {
            image.createNewFile();
        } catch (IOException e) {
            e.printstacktrace();
        }
    }
    // Create an File Uri
    return Uri.fromFile(image);
}


@Override
public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults) {
    switch (requestCode) {
        case MY_REQUEST_CODE: {
            // If request is cancelled,the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // permission was granted,yay! Do the
            // contacts-related task you need to do.
                if (ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this,MY_REQUEST_CODE_STORAGE);
                }else{
                    currentimageUri = getimageFileUri();
                    Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intentPicture.putExtra(MediaStore.EXTRA_OUTPUT,currentimageUri); // set the image file name
                    // start the image capture Intent
                    startActivityForResult(intentPicture,REQUEST_CAMERA);
                }
            } else {
                // permission denied,boo! disable the
                // functionality that depends on this permission.
                Toast.makeText(this,"Doesn't have permission... ",Toast.LENGTH_SHORT).show();
            }
            return;
        }
        case MY_REQUEST_CODE_STORAGE : {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                currentimageUri = getimageFileUri();
                Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intentPicture.putExtra(MediaStore.EXTRA_OUTPUT,currentimageUri); // set the image file name
                // start the image capture Intent
                startActivityForResult(intentPicture,REQUEST_CAMERA);
            } else {
                // permission denied,"Doesn't have permission...",Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}

Nougat这里有什么问题.是因为getimageFileUri()返回的uri?请帮我解决这个问题.

解决方法

嘿,请关注 this thread作为参考.当您将targetSDK设置为24并更改以下内容时,它将向您展示如何使用文件提供程序.在你的私有静态Uri getimageFileUri()方法

改变这一行

return Uri.fromFile(image);

FileProvider.getUriForFile(context,context.getApplicationContext().getPackageName() + ".provider",createImageFile());

希望这能帮助您解决问题.
欲了解更多,请访问 –
Setting Up File Sharing – Offical documentation

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...