一,执行拍照
/* * 开始拍照 * */ public void takePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //照片保存目录 file = new File(Environment.getExternalStorageDirectory(), STORAGE_PICTURE_NAME + System.currentTimeMillis() + ".jpg"); if (Build.VERSION.SDK_INT >= 24) { fileUri = FileProvider.getUriForFile(this, this.getPackageName() + ".provider", file); } else { fileUri = Uri.fromFile(file); } intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); }
二,拍照返回
case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE: //拍照返回 File destFile = new File(Environment.getExternalStorageDirectory().getPath(), COMPRESS_PICTURE_NAME + System.currentTimeMillis() + ".jpg"); File compressFile = BitmapUtil.compressPicture(this.file.getAbsolutePath(), destFile.getAbsolutePath(), 500, 500); FileUtils.deleteFileByName(Environment.getExternalStorageDirectory().getPath(), STORAGE_PICTURE_NAME); //TODO MLog.e("date", "压缩路径:" + destFile.getAbsolutePath()); Bitmap bitmap = BitmapFactory.decodeFile(compressFile.getAbsolutePath()); mImageLists.add(compressFile.getAbsolutePath()); break;
三,配置FileProvider
FileProvider作用:Android7.0后,通过它在应用建共享文件
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mbox.cn.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/> </provider>
在res下新建xml文件夹,新建filepaths文件
<?xml version="1.0" encoding="utf-8"?> <paths> <root-path path="." name="external_storage_root" /> </paths>