0

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!

6
  • Use one of the image-loading libraries for Android, such as Picasso. They handle the background threading and such for you. Commented Mar 31, 2016 at 20:30
  • @CommonsWare is there no other option other than using external libraries. I'm new to using libraries. Could you please help me understand? Thanks. Commented Mar 31, 2016 at 20:34
  • There are a lot of tutorials on using Picasso in Android. In fact, last I checked, their git repo had a few examples in the README Commented Mar 31, 2016 at 20:35
  • @zgc7009 Thanks! I'll check it out Commented Mar 31, 2016 at 20:38
  • "is there no other option other than using external libraries" -- sure there are. However, image management is complex: background processing, scaling, caching, tracking as views get recycled in things like ListView and RecyclerView, and so on. Libraries have implementations of all of that, written by Android experts, that have been tested by those experts and thousands of other Android developers through the use of those libraries. If you think that you can do better than that for your use case, feel free to implement yet another set of image-loading code for your own use. Commented Mar 31, 2016 at 20:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

But I cannot use InputStream is = (InputStream) new URL(strPhotoUrl).getContent(); This in onPostExecute
No, you download the image in the background thread and use that image in post execute.
As I mentioned in the question I cannot do so in doInBackground() because I don't have the URL then. I have to parse the URL in onPostExecute(). Thats why. So do I have to use ANOTHER AsyncTask?
Well, yes. Image downloading cannot happen on the main UI thread. But, why can't you parse the URL in the background thread and get the image downloaded? Why do you need to do it in post execute? #justcurious
yes. I got. I was just doing that when you posted this comment. I am new to AsyncTask. I didn't know if I could parse JSON there. That's why. Sorry. Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.