[Android development] Get an image from the server in Java and set it in ImageView! !!


It is a method to refer to the image file by URL and get it from the server. (Asynchronous processing)



import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageUrl extends AsyncTask<Integer, Integer, Bitmap> {

    ImageView bmImage;

    public ImageUrl(ImageView bmImage) {
        this.bmImage = bmImage;

    public Bitmap doInBackground(Integer... integers) {
        Bitmap image;
        URL imageUrl = null;
        try {
            //Change the URL to get with the argument number
            //Let's define the URL of the image as a static variable
            if (integers[0] == 0) {
                imageUrl = new URL(Constants.Image1);
            } else if (integers[0] == 1) {
                imageUrl = new URL(Constants.Image2);
            } else if (integers[0] == 2) {
                imageUrl = new URL(Constants.Image3);
            InputStream imageIs;
            imageIs = imageUrl.openStream();
            image = BitmapFactory.decodeStream(imageIs);
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        return image;

    protected void onPostExecute(Bitmap result) {



 int ImageNum;
 //Get the target image with the numerical value contained in ImageNum and set it in ImageView
 new ImageUrl((ImageView) view.findViewById(R.id.any_image)).execute(ImageNum);

Recommended Posts

[Android development] Get an image from the server in Java and set it in ImageView! !!
JSON in Java and Jackson Part 1 Return JSON from the server
Get attributes and values from an XML file in Java
[Android Studio] Set an arbitrary image for the application background [Java]
Get history from Zabbix server in Java
Get YouTube video information with Retrofit and keep it in the Android app.
[Java] Program example to get the maximum and minimum values from an array
Get the value from the array and find out what number it is included in
Implement Java Interface in JRuby class and call it from Java
[Kotlin] Get Java Constructor / Method from KFunction and call it
Easily get an integer from a system property in Java
Correct the character code in Java and read from the URL
Get a non-empty collection from an Optional stream in java
Get an image as a character string (resource name) and paste it into ImageView (kotiln)
Install the memcached plugin on MySQL and access it from Java
[Java] Get the dates of the past Monday and Sunday in order
Code to specify the image name as a character string and paste it into ImageView (android)
[Java] How to convert from String to Path type and get the path
[Android] I want to get the listener from the button in ListView
How to get the length of an audio file in java
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
JSON in Java and Jackson Part ③ Embed JSON in HTML and use it from JavaScript
[Java] Get Json from URL and handle it with standard API (javax.script)
Add the pre-built jar library to Android and call it in the framework
Gradle automatically generates version number from git and uses it in Java
[Swift] How to set an image in the background without using UIImageView.
Is it possible to put the library (aar) in the Android library (aar) and use it?
How to create your own annotation in Java and get the value
Kick ShellScript on the server from Java
How to get the date in java
[Java8] Search the directory and get the file
Java to C and C to Java in Android Studio
What did I prepare for when I entered an Android application development project while developing the Web in Java?
Try adding text to an image in Scala using the Java standard library
I received the data of the journey (diary application) in Java and visualized it # 001
Java: Download the file and save it in the location selected in the dialog [Use HttpClient]
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Summarize the life cycle of Java objects to be aware of in Android development
Impressions and doubts about using java for the first time in Android Studio
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
I stumbled on the Java version in Android Studio, so I will summarize it
[Java] Understand the difference between List and Set
Regarding the transient modifier and serialization in Java
[Java] Set the time from the browser with jsoup
How to get Class from Element in Java
Capture and save from selenium installation in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
[Java] Get a random value from an array
[Android] Get random keys and values from HashMap
Get your version number in the Android app
Put the image obtained from http in NotificationCompat
Minecraft BE server development from PHP to Java
Represents "next day" and "previous day" in Java / Android
Generate OffsetDateTime from Clock and LocalDateTime in Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
[Java] How to get the key and value stored in Map by iterative processing
[AWS Lambda] Resize the image saved in S3 and save it in another S3 (using Ruby)
[Java] Get metadata from files with Apathce Tika, and get image / video width and height from metadata [Kotlin]
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
Get the URL of the HTTP redirect destination in Java