I'm trying to make an app that detects motion and takes picture when the motion is detected. Its saving the picture when I don't try to save it in the directory(folder). But when I try it with the directory, the picture is not being saved even though the directory is being created successfully. What changes should I make to the following code in order to make it work:
 private void createDirectoryAndSaveFile(String name, Bitmap bitmap) {
        File folder = new File(Environment.getExternalStorageDirectory() +
            File.separator + "XYX APP");
             boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
        if (success) {
        // Do something on success
    } else {
        // Do something else on failure
    }
        File photo = new File(new File(Environment.getExternalStorageDirectory()+"XYZ APP/"), name+ ".jpg");
        if (photo.exists()) {
            photo.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(photo.getPath());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Note: The file name is generated in the following instruction:
 String name = "MotDet_"+String.valueOf(System.currentTimeMillis());
            if (bitmap != null) createDirectoryAndSaveFile(name, bitmap);
Update It works with the following code but not with the code above :
    private void save(String name, Bitmap bitmap) {
        File photo = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
        if (photo.exists()) photo.delete();
        try {
            FileOutputStream fos = new FileOutputStream(photo.getPath());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        } catch (java.io.IOException e) {
            Log.e("PictureDemo", "Exception in photoCallback", e);
        }
    }
				
                        
First of all you missed the FileSeperator before xyz
And your Function becomes
}
Marshmello comes with RuntimePermissions in order for you to save file in external directory you need to ask permission first, like below code
}
permission result callback
before saving call
isStoragePermissionsGranted()if it returnstrueproceed saving file.