1

I know this issue as been ask many times but i've tried many solutions and no one works.

On Android, I'm trying to get an image from URL to put it in an image view. Unfortunately, I get the following error :

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: http:/lorempixel.com/1920/1920/business (No such file or directory)

When I try to reach this URL from the browser of the emulator, it works.

I've already tried the following solutions : Load image from url

How to get image from url website in imageview in android

how to set image from url for imageView

My actual code is the following :

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

    ImageView imgView;

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

    @Override
    protected Bitmap doInBackground(String... urls) {
        return download_Image(urls[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result != null)
            imgView.setImageBitmap(result);
    }


    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
                Log.e("CON ", "HTTP connection OK");
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }


    private Bitmap download_Image(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection("http://lorempixel.com/1920/1920/business");
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;

    }
}

In the main activity my code is :

 ImageView img = (ImageView) findViewById(R.id.imageView);
 DownloadImage download = new DownloadImage(img);
 download.execute(url);

In the manifest I've added :

<uses-permission android:name="android.permission.INTERNET" />

What can I do ?

Community
  • 1
  • 1
anais1477
  • 466
  • 1
  • 17
  • 36
  • In which device OS you are testing this?? – Vishal Senjaliya May 16 '17 at 13:07
  • Try this library : https://github.com/bumptech/glide – schinj May 16 '17 at 13:13
  • `http://lorempixel.com/1920/1920/business` results in an HTTP 301 response, redirecting you to `http://lorempixel.com/1920/1920/business/`. Your `setInstanceFollowRedirects(true)` should handle that, but I rarely use `HttpURLConnection`, so I do not know if there are any issues there. As with Sac, I recommend that you use [an image-loading library](https://android-arsenal.com/tag/46) (e.g., Picasso). – CommonsWare May 16 '17 at 13:16
  • So I tried with the `http://lorempixel.com/1920/1920/business/` but I have the same error. I can't use any external library because it's a school exercise. I think the problem is that it tries to decodeStream before the HttpConnection is OK. How can I force it to wait ? – anais1477 May 16 '17 at 13:49

1 Answers1

0

This works perfect:

String urlPath = "http://anyWebsite.com/images/12"
Bitmap bm = BitmapFactory.decodeStream((InputStream) new URL(urlPath).getContent());
myImageView.setImageBitmap(bm);
vss
  • 1,093
  • 1
  • 20
  • 33