How can I save an images to the SD card that I retrieve from the image's URL?
Check If the SD card Has Read or Write Error One of the reasons why you can't move files to SD card is a read and write error. One way to check if this error is the reason behind you being unable to move files to SD card is to set your phone camera to store the images and videos taken from it directly to the SD card.
Transfer Files from SD Card to Phone Memory SamsungOpen the My Files app and click the SD Card option. Select and enter the folder that is stored the files you want to transfer like Pictures, Music, etc. Long-press the file you want to transfer and tap on Move or Copy.
First you must make sure your application has permission to write to the sdcard. To do this you need to add the uses permission write external storage in your applications manifest file. See Setting Android Permissions
Then you can you can download the URL to a file on the sdcard. A simple way is:
URL url = new URL ("file://some/path/anImage.png"); InputStream input = url.openStream(); try { //The sdcard directory e.g. '/sdcard' can be used directly, or //more safely abstracted with getExternalStorageDirectory() File storagePath = Environment.getExternalStorageDirectory(); OutputStream output = new FileOutputStream (new File(storagePath,"myImage.png")); try { byte[] buffer = new byte[aReasonableSize]; int bytesRead = 0; while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, bytesRead); } } finally { output.close(); } } finally { input.close(); }
EDIT : Put permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With