String fileName = Environment.getExternalStorageDirectory() + File.separator + “DCIM”
- File.separator + “demo” + File.separator;
try {
File file = new File(fileName);
if (!file.exists()) {
file.mkdirs();
}
MLog.e(“saveAndGetImage:” + file);
File filePath = new File(file + “/” + imgName + “.png”);
MLog.e(“filePath:” + filePath);
FileOutputStream out = new FileOutputStream(filePath); //保存到本地,格式为JPEG
if (outB.compress(Bitmap.CompressFormat.PNG, 100, out)) {
out.flush();
out.close();
}
MLog.e(“saveAndGetImage:END”);
//刷新图库
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//高于22版本要手动授权
// 检查该权限是否已经获取
int i = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
// 权限是否已经 授权 GRANTED—授权 DINIED—拒绝
if (i != PackageManager.PERMISSION_GRANTED) {
// 提示用户应该去应用设置界面手动开启权限
} else {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(filePath)));
}
} else {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(filePat