0

I am trying to build a simple app (a button that downloads an image and imageView) that downloads an image but the image is not being displayed in the app. The app runs without any errors but the Image is not being displayed. Here is my MainActivity code. Thanks much.

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    public void downloadImage(View view){
        ImageDownloader task = new ImageDownloader();
        Bitmap myImage;
        try{
            myImage = task.execute("https://wallpapercave.com/wp/wp4410880.jpg").get();

            imageView.setImageBitmap(myImage);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
    }
    public class ImageDownloader extends AsyncTask<String, Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... urls) {
            try {
                URL url = new URL(urls[0]);

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                connection.connect();

                InputStream in = connection.getInputStream();

                Bitmap myBitmap = BitmapFactory.decodeStream(in);

                return myBitmap;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

1 Answer 1

1

try that way

Define method onPostExecute() in the Download task

and call setImageBitmap in it, this methods gets called after the doinbackgroundon the main thread

do it like the following

public class ImageDownloader extends AsyncTask<String, Void, Bitmap>{

    @Override
    protected Bitmap doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.connect();

            InputStream in = connection.getInputStream();

            Bitmap myBitmap = BitmapFactory.decodeStream(in);

            return myBitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
   public void onPostExecute(Bitmap myImage){ //the returned value of `doInbackground` gets here
      imageView.setImageBitmap(myImage);
 }
}

Also please do not call get() method on the task, just say new DownloadTask().execute();

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

5 Comments

"task.execute("https://wallpapercave.com/wp/wp4410880.jpg").get(); is asynchronous your code you keep moving on after calling this method." – Actually, the get() call makes it synchronous, which is likely leading to a NetworkOnMainThreadException that they're ignoring. Your solution will fix that, too, though; you were just mistaken about the specific root cause. Just FYI. Cheers!
thanks, i never used get(), but i just knew that how to make it work, if get() makes it synchronous leading to NOMTE , there is not exception as OP says , so please provide and answer ,about problem in OP's code,and please inform me i would also like to learn, i am also editing my answer.
Yeah, they're probably just not familiar with logcat, yet, so they don't even know that an Exception is being thrown, because they're catching everything, and doing nothing with it. Anyhoo, you've got right way to do it. No need for me to distract the OP with another answer. :-) Cheers!
ok sir, because get makes it synchronous and is cause of exception i think there should be some listener which can be called after that, right?
Sorry, I'm not really sure what you're asking, but you've got the right code setup. However, I think you meant "Also please do not call get() method...".

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.