N
N
Nick20152015-08-17 15:52:36
Android
Nick2015, 2015-08-17 15:52:36

Open(show) Image from URL?

you need to open an image from the Internet by URl and display it in an ImageView.
Tried many options
1. Here is one example

String stringURL = "http://icons.iconarchive.com/icons/icons8/ios7/72/Messaging-Lol-icon.png";


        InputStream is = null;
        BufferedInputStream bis = null;
        Bitmap bmp = null;

        try {
            URL url = new URL(stringURL);
            URLConnection conn = url.openConnection();
            conn.connect();
            is = conn.getInputStream();
            bis = new BufferedInputStream(is);
            bmp = BitmapFactory.decodeStream(bis);

        }catch (Exception ignored) {

        } finally {
            try {
                if( is != null )
                    is.close();
                if( bis != null )
                    bis.close();
            } catch (IOException e) {

            }
        }
        imageView.setImageBitmap(bmp);

I have already reviewed everything on stackoverflow.com
. I used Picasso.
But it's empty all the time
2. here's another code
try {
        URL url = new URL("http://icons.iconarchive.com/icons/icons8/ios7/72/Messaging-Lol-icon.png");
        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        imageView.setImageBitmap(bmp);
        } catch (IOException e) {
            e.printStackTrace();
        }

3. But I would like to do it this way
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = 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.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

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

and in onCreate()
new DownloadImageTask(imageView)
                .execute(stringURL);

what and where is wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nick2015, 2015-08-18
@Nick2015

Guys, this is just tin. I started to feel like a moron. It seems that with the codes (I tried Not 1) everything is fine and *** did not do them, gave UnknownHostException. Everything was written in the manifesto.
Stupid mistake, I thought that everything was fine with the manifest,
that's how it was
that's how it was and everything works
Spent a day through such a stupid mistake
. Thank you for trying to help. I already got you

E
Emin, 2015-08-17
@Ewintory

You've gone the wrong way, with Picasso you don't need any tasks. Read the documentation carefully.

V
Vitaly Pukhov, 2015-08-18
@Neuroware

What writes in the error if it is not ignored here } catch (Exception ignored) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question