File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture");
if (! path.exists()) {
path.mkdirs();
if (!path.exists()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.CHINA).format(new Date());
File imagePath = new File(path.getPath() + "_" + "IMG_" + timeStamp + ".jpg");
BufferedOutputStream fos;
try {
fos =new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return imagePath;
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
return null;
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
return null;
}
And in manifest the permission set is right
This issue is in Android Pie and higher version. So, adding this line in manifest file fixed error.
<application
...
...
android:requestLegacyExternalStorage="true">
</application>