1

I need to get image from the web and store it in the phone for later use.

I tryed this:

 public Drawable grabImageFromUrl(String url) throws Exception
    {
     return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
    }

So this my function to grab image from Url, i just need a proccess to get the returned drawable and save.

How can i do that ?

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

2 Answers2

3

Based off here, you can actually download the image using a different method. Is it absolutely necessary that you store it as a drawable before saving it? Because I think you could save it first, and THEN open it, if need be.

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()
    String storagePath = Environment.getExternalStorageDirectory();
    OutputStream output = new FileOutputStream (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();
}
Community
  • 1
  • 1
ravemir
  • 1,153
  • 2
  • 13
  • 29
3

see this complete example give here

http://android-example-code.blogspot.in/p/download-store-and-read-images-from.html

MAC
  • 15,799
  • 8
  • 54
  • 95