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?