Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String timeStamp = new SimpleDateFormat("yyyyMMddmmss").format(new Date()); String imgFileName = "IMG_" + timeStamp; //核心 不可使用 Environment.getExternalStorageDirectory() String path = context.getExternalCacheDir().getAbsolutePath() + File.separator + "images" + File.separator+ "photos"; File file = new File(path); if (!file.exists()) { file.mkdirs(); } File cameraFile = new File(file.getPath(),imgFileName) Uri fileUri; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { fileUri = Uri.fromFile(cameraFile); } else { fileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".update.provider", cameraFile); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //添加这一句表示对目标应用临时授权该Uri所代表的文件 takeIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); mContext.startActivityForResult(takeIntent, 100);相册
Intent intentPhoto = new Intent(Intent.ACTION_PICK); intentPhoto.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); mContext.startActivityForResult(intentPhoto, 100);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)