Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I transfer an image from its URL to the SD card?

Tags:

How can I save an images to the SD card that I retrieve from the image's URL?

like image 245
Jameskittu Avatar asked Jul 21 '10 06:07

Jameskittu


People also ask

Why can't I move my files to SD card?

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.

How do I move pictures from my Android to my 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.


1 Answers

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" /> 
like image 163
Akusete Avatar answered Sep 26 '22 03:09

Akusete