Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:35:39+00:00 2026-06-17T05:35:39+00:00

Hi so i’m trying to grab a image from a url link via AsyncTask,

  • 0

Hi so i’m trying to grab a image from a url link via AsyncTask, the function to grab the image itself work fine. but what i trying to do is to pass the src variable into a asyncTask which seems to be not working for me. the return will be blank.

here is the code:

 private AsyncTask<String, Void, Drawable> task2;
 Drawable profile;
 public Drawable getProfile(String src){        
    task2 = new AsyncTask<String, Void, Drawable>() {           
        ProgressDialog dialog2;
        InputStream is;
        Drawable d;
        @Override 
        protected void onPreExecute(){
            dialog2 = new ProgressDialog(Thoughts.this, ProgressDialog.STYLE_SPINNER);
            dialog2.setMessage("Loading Data...");          
            dialog2.setCancelable(false);
            dialog2.setCanceledOnTouchOutside(false);   
            dialog2.show();
        }
        @Override
        protected Drawable doInBackground(String... src) {
                try
                {
                    is = (InputStream) new URL(src[0]).getContent();
                    d = Drawable.createFromStream(is, "src name");
                    return d;
                }catch (Exception e) {
                    e.toString();
                    return null;
                }
        }
        @Override
        protected void onPostExecute(Drawable result2) {
            profile = result2; 
            dialog2.dismiss();
        }
    };
    task2.execute(src);
    return profile;
}

and i call it like this at the onCreate();

Drawable p4 = getProfile("http://..../xyz.jpg");
Drawable p5 = getProfile("http://..../xyz.jpg");

ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
ImageView thoughtsProfilePic1 =(ImageView) findViewById(R.id.ivProfilePicData1);

thoughtsProfilePic.setImageDrawable(p4);
thoughtsProfilePic1.setImageDrawable(p5);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:35:40+00:00Added an answer on June 17, 2026 at 5:35 am

    AsyncTask help you do an asynchronous job. In your code, I can see you return Drawable right after calling it. But at that moment, the your asynctask hasn’t completed yet and drawable still null.

    task2.execute(src);
    return profile;
    

    If you want set drawable resource when complete job in asynctask, just put your ImageView into your method. It should be:

      public void getProfile(String src, final ImageView v){           
    
             @Override
        protected void onPostExecute(Drawable result2) {
    
            // set drawable for ImageView when complete.
            v.setImageDrawable(result2);
            dialog2.dismiss();
        }
        task2.excute(src);
        //do not need return anything.
      } 
    

    Use it:

     ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
     getProfile("http://..../xyz.jpg", thoughtsProfilePic );
    

    Hope this help.

    Update:
    There is no way to return value from asynchronous method directly, here is another choice.
    First, create an interface to notify when complete job.

     public interface INotifyComplete{  
          public void onResult(Drawable result);  
     } 
    

    Then your activity class should look like:

     public class YourActivity extends Activity implement INotifyComplete{
     private Drawable res1;
     private Drawable res2;
     public void onResult(Drawable result){
        if(result == res1){
            // do something with resource 1
            ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
            thoughtsProfilePic.setImageDrawable(result);
        }
        if(result == res2){
            // do something with resource 2
        }
      }
    
     void someMethod(){
    // you can use this way to call
        getProfile("http://..../xyz.jpg", res1, YourActivity.this);
        //or this
        getProfile("http://..../xyz.jpg", res2, new INotifyComplete(){
            public void onResult(Drawable result){
                // result is res2, so don't need to check
            }
        });
     }
     public void getProfile(String src, final Drawable res, final INotifyComplete notify){
       //don't need store asynctask instance
        AsyncTask<String, Void, Drawable>  task2 = new AsyncTask<String, Void, Drawable>(){
    
            // do something ...
    
            protected void onPostExecute(Drawable result2) {             
            dialog2.dismiss();
            // you will set the value to your drawable then notify it when complete job
            res = result2;
            notify.onResult(res);
        }
        }
        task2.excute();
    }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.