一.权限
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
分别代表调用相机的权限和内存读写的权限
二.点击事件
public void AddPic(View view) {
CharSequence[] items = {"相册", "相机"};
new AlertDialog.Builder(this)
.setTitle("选择图片来源")
.setItems(items, new DialogInterface.OnClickListener() {
public static final int SELECT_CAMER = 1;
public static final int SELECT_PICTURE = 0;
public void onClick(DialogInterface dialog, int which) {
if (Utils.getAndroidSDKVersion() < 19) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE);
} else {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
}
} else {
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//path为保存图片的路径,执行完拍照以后能保存到指定的路径下
File file;
private String picpath = "/storage/emulated/0/DCIM/Camera/", picfilename = "IMG_picpath1.jpg";
file = new File(picpath, picfilename);
if (Utils.getAndroidSDKVersion()<24){//安卓7.0以后版本,只接受content uri 不能再