KISHORE_ZE
KISHORE_ZE

Reputation: 1476

How to retrieve an image from an URL? Errors

I am parsing a JSON Obj to get the url of an image. I am using this code.

private Drawable LoadImageFromWebOperations(String strPhotoUrl) {
    try {
        InputStream is = (InputStream) new URL(strPhotoUrl).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        Log.e("TAGG", strPhotoUrl);
        return d;
    } catch (Exception e) {
        Log.e("TAGG", e.toString());
        return null;
    }
}

But I get an error that this must be done in an AsyncTask,i.e., Different Thread. But then how will I insert the drawable in an ImageView? Since DoInBackground() dosen't have access to UI elements. Also I don't have access to the URL until I parse the JSON obj I get the URL from. So what solution can I use in my situation. Thanks!

Upvotes: 0

Views: 101

Answers (1)

Swayam
Swayam

Reputation: 16364

You can access the UI elements from onPostExecute().

So, you can make the API call and get the data in the background thread. After the image has been downloaded, you can set the image to the ImageView in the onPostExecute() method.

Upvotes: 1

Related Questions