Add :
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
In your manifest file, otherwise the source cannot be downloaded.
If you want to save it in sd card you need another permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In order to save it.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If you want to get that file in the future.
Next if you want to save it to an external directory, you can try this:
public void setUpDirectory(String folderName,String source){
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+folderName);
if(!directory.exists() && !directory.isDirectory()) {
directory.mkdirs();
}
URL imageurl = new URL(source);
Bitmap bitmap = BitmapFactory.decodeStream(imageurl.openConnection().getInputStream());
saveFile(directory,bitmap);
}
private void saveFile(File fileName,Bitmap bmp){
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(fileName);
bmp.compress(Bitmap.CompressFormat.PNG, 100, outputStream ); // 100 will be ignored
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if outputStream != null) {
outputStream .close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}