The following code which consists of downloading a file from a server and save it in the storage works fine when the device has an internal storage.
But when I tried it
Did you try it on emulator? Check the properties if it has an SD card. I had the same problem, and it was because the emulator did not have an SD card. Check if yours has or not.