0
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)
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
  • `dir.mkdirs();` Better: `if(!dir.exists())if(!dir.mkdirs()) return;` Also display a Toast to inform the user. – blackapps Jun 18 '21 at 06:32
  • From your code, it seems like you trying to save an image. Try the following code https://stackoverflow.com/a/68148143/9846650 – mad_lad Jun 27 '21 at 05:08

2 Answers2

0

getExternalFilesDir() flag is deprecated in Android 11. You need to used cache storage or media storage.

0

Use the following code to check weather your version is >= Android 11, the getExternalStorageDirectory() will not work you have use getExternalFilesDir() Flag. In the below function there is Boolean will return wether the file exist in external storage or not.

 public boolean checkImageInLocalStorage(String fileName, String folderName, String folderType){
    boolean isPresent =false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
        File rootPath= context.getExternalFilesDir("AppFolderName").getAbsoluteFile();
        String rootFolder =Constants.DOWNLOAD_DIRECTORY_NAME;

        File localFile = new File(rootPath+"/"+rootFolder+"/"+folderType+"/"+folderName+"/"+fileName);
        if (localFile.exists() && !localFile.isDirectory()){
            isPresent = true;
        }
    }else {

        File rootPath = Environment.getExternalStorageDirectory();
        String root = rootPath.getAbsolutePath().toString();
        String rootFolder = Constants.DOWNLOAD_DIRECTORY_NAME;

        //  File rootPath=context.getFilesDir();
        File localFile = new File(rootPath.getAbsoluteFile().getAbsolutePath());
        localFile = new File(root + "/" + rootFolder + "/" + folderType + "/" + folderName + "/" + fileName);

        if (localFile.exists() && !localFile.isDirectory()) {
            // do something
            isPresent = true;
        }
    }
    return isPresent;
}
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Jun 18 '21 at 08:54