Adam Varhegyi
Adam Varhegyi

Reputation: 9894

Get image from web and save it to phones memory ?

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 ?

Upvotes: 1

Views: 3889

Answers (2)

ravemir
ravemir

Reputation: 1163

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();
}

Upvotes: 3

MAC
MAC

Reputation: 15847

see this complete example give here

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

Upvotes: 3

Related Questions