大佬教程收集整理的这篇文章主要介绍了Android:如何在android nougat中编写相机意图,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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; } } }
改变这一行
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,请注明来意。