Saturday, April 28, 2012

How to download an image from a URL


When you need to download a image from a URL to your directory, use this simple tip:

File image = new File(path + "//image.png.jpg.gif.bmp");

if (!image.exists()) {
  URL url = new URL("image_URL");
  InputStream in = url.openStream();
  OutputStream out = new BufferedOutputStream(new FileOutputStream(image));
  for (int b; (b = in.read()) != -1;) {
       out.write(b);
      }
  out.close();
  in.close();
}

No comments:

Post a Comment