Search code examples
javaandroidimagegalleryfileoutputstream

Android save FileOutputStream in gallery


I have an image created with FileOutputStream and I try to save it to gallery. I was looking for stack overflow answers and everywhere else and nothing works for me

My code:

File path = MainActivity.this.getExternalFilesDir("ComputerScreenshots");
FileOutputStream ops = new FileOutputStream(path + "/Computer.png");

The code above saves the image in /storage/emulated/0/Android/data/com.example.computer/files/ComputerScreenshots/Computer.png

How can I save it to gallery?


Solution

  • I think below code will help you.

    File path = new File( Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_DCIM), "ComputerScreenshots");
    

    Check your directory is existing or not? if not then make it.

    if (!path.exists())
            path.mkdir()
    

    If you want to save your Image into Camera Directory then Replace ComputerScreenshots to Camera