Hello! It is required to implement the saving of images to the device gallery. At the moment I use this code to get the path to this folder:

public class FileUtil { private static final String APP_DIRECTORY_NAME = "lounah"; private static final File savedPhotosExternalDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), APP_DIRECTORY_NAME); private static final File savedPhotosInternalDir = new File(Environment.getDataDirectory(), APP_DIRECTORY_NAME); private FileUtil() {} // TODO: fix crush, when external is unavailable public static File savedPhotosDirectory() { File savedPhotosDir = null; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (!savedPhotosExternalDir.exists()) savedPhotosExternalDir.mkdir(); savedPhotosDir = savedPhotosExternalDir; } else if (!savedPhotosInternalDir.exists()) { savedPhotosInternalDir.mkdir(); savedPhotosDir = savedPhotosInternalDir; } return savedPhotosDir; } 

}

The problem is that this code works great if the device provides for an SD card, but if it doesn't exist (Google Pixel 2, Sony Xperia A1, for example), then there is no externalStorageDirectory either. Where in this case, save the image?

  • It's obvious - you need to save in the internal memory. And in general, you should always do this. It is up to the user to decide where to save the photos, not your application. - Enikeyschik

1 answer 1

If the device does not have External Storage (external memory), then it can be stored in the Internal Storage (internal memory of the device).

You can read more in the official documentation.