前言:
Android M相比6.0以前的需要权限,其他的跟之前的没区别:
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)|| (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { //如果没有授权,则请求授权 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } else {而7.0开始,为了安全,需要使用FileProvider了,就不能用上面那行红色代码了:// create Intent to take a picture and return control to the calling application Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image File file = new File(Environment.getExternalStorageDirectory().getPath(), "camera.png"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); // set the image file name // start the image capture Intent startActivityForResult(intent, 1);
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)|| (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { //如果没有授权,则请求授权 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } else { //有授权,直接开启摄像头 //6.0 camera /*Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File("/mnt/sdcard", "camera.png"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); // set the image file name startActivityForResult(intent, 1);*/ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager())!=null){ File photoFile = null; try { photoFile = createImageFile();//创建临时图片文件,方法在下面 } catch (IOException e) { e.printStackTrace(); } if (photoFile != null) { //FileProvider 是一个特殊的 ContentProvider 的子类, //它使用 content:// Uri 代替了 file:/// Uri. ,更便利而且安全的为另一个app分享文件 Uri photoURI = FileProvider.getUriForFile(this, "com.sat.android.fileprovider", photoFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(intent, 1); } } }createImageFile():
String mCurrentPhotoPath; private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; //.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据 File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); /*File storageDir = new File(Environment.getExternalStorageDirectory(), "images"); if (!storageDir.exists()) storageDir.mkdirs();*/ Log.d("TAH",storageDir.toString()); //创建临时文件,文件前缀不能少于三个字符,后缀如果为空默认未".tmp" File image = File.createTempFile( imageFileName, /* 前缀 */ ".jpg", /* 后缀 */ storageDir /* 文件夹 */ ); mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image; }然后需要在manifest文件中配置:
<provider android:authorities="com.sat.android.fileprovider" //与FileProvider.getUriForFile第二个参数相同新建/res/xml/file_paths:android:name="android.support.v4.content.FileProvider"android:exported="false" android:grantUriPermissions="true"><meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/></provider>
<paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="Android/data/com.sat.a10_6_/files/Pictures" /> </paths>注意:path的值要与
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);的值一样,省略前面的 "/storage/emulatored/0/",不然会奔溃,我也不知道为什么,再研究研究。。。