FileOutputStream outputStream = null;
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//String string = Environment.getExternalStorageDirectory().toString();
File dir = new File(file + "/MyPics");
dir.mkdirs();
String filename = String.format("%d.jpg",System.currentTimeMillis());
Log.e(TAG," saveToGallery filename" + filename);
File outFile = new File(dir,filename);
Log.e(TAG," saveToGallery outFile " + outFile.toString() + " Output file exists : " + outFile.exists());
if (outFile.exists ()) {
outFile.delete();
Log.e(TAG," saveToGallery outFile.delete() " );
}
if(outFile.createNewFile()) {
Log.e(TAG, " saveToGallery outFile.createNewFile() " + outFile.toString() + " Output file exists : " + outFile.exists());
try {
outputStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
Log.e(TAG, " saveToGallery outputStream " + outputStream.toString());
} catch (Exception e) {
Log.e(TAG, " saveToGallery Creating outputStream failed ");
e.printStackTrace();
}
Error Log:
2020-11-04 05:07:38.479 24501-24501/com.mtech.mtechproject E/ checkPermission Function: Granted permission for writing: 101
2020-11-04 05:07:38.479 24501-24501/com.mtech.mtechproject E/BaseActivity: saveToGallery ++
2020-11-04 05:07:38.486 24501-24501/com.mtech.mtechproject E/BaseActivity: saveToGallery filename1604446658485.jpg
2020-11-04 05:07:38.488 24501-24501/com.mtech.mtechproject E/BaseActivity: saveToGallery outFile /storage/emulated/0/Pictures/MyPics/1604446658485.jpg Output file exists : false
2020-11-04 05:07:38.490 24501-24501/com.mtech.mtechproject W/System.err: java.io.IOException: No such file or directory
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at java.io.File.createNewFile(File.java:1008)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at com.mtech.mtechproject.BaseActivity.saveToGallery(BaseActivity.java:306)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at com.mtech.mtechproject.BaseActivity.checkPermission(BaseActivity.java:266)