SDK 24 或更高版本也支持从图库中选择照片并拍摄新照片的代码
活动
private File filePathImageCamera;
private Uri imagePath;
private static final int IMAGE_GALLERY_REQUEST = 2;
private static final int IMAGE_CAMERA_REQUEST = 3;
private String imageFilePath;
private void selectImage() {
final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(DocumentActivity.this);
builder.setTitle("Upload Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
photoCameraIntent();
} else if (options[item].equals("Choose from Gallery")) {
photoGalleryIntent();
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void photoCameraIntent() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = null;
try {
filePathImageCamera = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (pictureIntent.resolveActivity(getPackageManager()) != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//Create a file to store the image
if (filePathImageCamera != null) {
Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", filePathImageCamera);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
photoURI);
startActivityForResult(pictureIntent,
IMAGE_CAMERA_REQUEST);
}
} else {
if (filePathImageCamera != null) {
uri = Uri.fromFile(filePathImageCamera);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(pictureIntent, IMAGE_CAMERA_REQUEST);
}
}
}
}
private File createImageFile() throws IOException {
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir =
getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// Create the storage directory if it does not exist
if (!storageDir.exists() && !storageDir.mkdirs()){
Log.d("error", "failed to create directory");
}
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
imageFilePath = image.getAbsolutePath();
return image;
}
private void photoGalleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Get Image From"), IMAGE_GALLERY_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_GALLERY_REQUEST) {
if (resultCode == RESULT_OK) {
imagePath = data.getData();
Glide.with(MainActivity.this).load(imagePath).into(imageview);
}
} else if (requestCode == IMAGE_CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
if (filePathImageCamera != null && filePathImageCamera.exists()) {
imagePath = Uri.fromFile(filePathImageCamera);
Glide.with(MainActivity.this).load(imagePath).into(imageview);
}
}
}
}
显现
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
在 res/xml 目录中创建新文件 provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/your_package_name/files/Pictures" />
</paths>
注意 - 需要应用程序存储访问权限