解決代碼:
// 判斷存儲卡是否可以用,可用進行存儲
String sdStatus = Environment.getExternalStorageState();
if (sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//創建拍照後的存儲路徑
String path = Environment.getExternalStorageDirectory().toString() + "/Photo";
File path1 = new File(path);
if (!path1.exists()) {
path1.mkdirs();
}
String imageFileName = System.currentTimeMillis() + ".jpg";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果是7.0android系統
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, new File(path, imageFileName).getAbsolutePath());
imageFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
} else {
imageFileUri = Uri.fromFile(new File(path, imageFileName));
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(intent, action);
}else {
ToastUtils.showShort("SD卡不可用");
}
獲取拍照後圖片地址:
if (imageFileUri != null) {
//根據uri獲取圖片路徑
logoImg = BitmapUtil.getRealFilePath(getApplication(), imageFileUri);
}
同時不要忘了申請權限