-2

So, I have this app that is connected to a WebService and I am already retrieving data from there, now I want to retrieve a image link and make that the imageView gets that image trough the link. Is that even possible? Appreciate any help :D

        @Override
    protected Void doInBackground(Void... params) {
        HttpHandler sh = new HttpHandler();

        String jsonStr = sh.makeServiceCall(url);
        Log.e(TAG, "Response from URL: " + jsonStr);

        if (jsonStr != null) {
            try {

                JSONArray array = new JSONArray(jsonStr);
                for (int i = 0; i < array.length(); i++) {

                    JSONObject jsonObject = array.getJSONObject(i);

                    JSONArray paises = jsonObject.optJSONArray("paises");

                    if (paises != null) {
                        for (int j = 0; j < paises.length(); j++) {
                            JSONObject jsonObject1 = paises.getJSONObject(j);
                            System.out.println(jsonObject1.optString("Designacao"));
                            String K_PAIS = jsonObject1.getString("K_PAIS");
                            String Designacao = jsonObject1.getString("Designacao");
                            String URL_IMAGE_SMALL = jsonObject1.getString("URL_IMAGE_SMALL");
                            String URL_IMAGEM = "http://something.something.pt" + URL_IMAGE_SMALL;

                            new DownloadImage(imageView6).execute(URL_IMAGEM);

                            HashMap<String, String> pais = new HashMap<>();

                            pais.put("K_PAIS", K_PAIS);
                            pais.put("Designacao", Designacao);
                            pais.put("URL_IMAGE_SMALL", URL_IMAGE_SMALL);
                            pais.put("URL_IMAGEM", URL_IMAGEM);
                            listaPaises.add(pais);


                        }
                    }
                    System.out.println(jsonObject.optString("Designacao"));
                }



            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Json parsin error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
            }
        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errpr!", Toast.LENGTH_LONG).show();
                }
            });
        }
        return null;



    }
{...}
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImage(ImageView bmImage) {
        this.bmImage = (ImageView) bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.d("Error", e.getStackTrace().toString());

        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}
alb
  • 347
  • 2
  • 7
  • 24
  • 5
    Possible duplicate of [How to display image from URL on Android](http://stackoverflow.com/questions/6407324/how-to-display-image-from-url-on-android) – H4SN Mar 09 '17 at 12:11

6 Answers6

1

You can use Picasso, a wonderful image library.

Example:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Add dependency via Gradle:

compile 'com.squareup.picasso:picasso:2.5.2'
0

you can use any third party library example use Glide library

this library will help you to fetch and display image on ImageView from url.

example:

Glide.with(context).load(image_url).into(your_image_view);

here is link for that library : https://github.com/bumptech/glide

Learn Pain Less
  • 2,274
  • 1
  • 17
  • 24
0

You have to set your ImageView inside your XML as you normally do. Then you can use any third party library like Picasso or Glide that will load the image from the url and set it to your ImageView in your activity/fragment.

phoenix
  • 496
  • 3
  • 11
0

In your app build.gradle add

compile 'com.github.bumptech.glide:glide:3.7.0'

use this code to load image from url

 Glide.with(getApplicationContext()).load("image_url").into(ImageView);
H4SN
  • 1,482
  • 3
  • 24
  • 43
0

try this if you dont want to use third party library

 new DownloadImage(imamgeview).execute(url);

create a Async Task

 public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    CircleImageView bmImage;

    public DownloadImage(ImageView bmImage) {
        this.bmImage = (CircleImageView) bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.d("Error", e.getStackTrace().toString());

        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

i hope you it will work in your case

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

step 1: create class named DownloadImage

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    CircleImageView bmImage;

    public DownloadImage(ImageView bmImage) {
        this.bmImage = (CircleImageView) bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.d("Error", e.getStackTrace().toString());

        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

Step 2: execute AsyncTask

new DownloadImage(imgUserProfile).execute(userProfileUrl);

**Json Url like this: ** https://graph.facebook.com/1220130444748799/picture?height=400&width=400&migration_overrides=%7Boctober_2012%3Atrue%7D

ND1010_
  • 3,743
  • 24
  • 41