Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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?请帮我解决这个问题.

@R_502_1964@

嘿,请关注 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:如何在android nougat中编写相机意图全部内容,希望文章能够帮你解决Android:如何在android nougat中编写相机意图所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。