Get Image from Url Unable to Decode Stream: fileNotFoundException

匿名 (未验证) 提交于 2019-12-03 02:34:02

问题:

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 ?

回答1:

This works perfect:

String urlPath = "http://anyWebsite.com/images/12" Bitmap bm = BitmapFactory.decodeStream((InputStream) new URL(urlPath).getContent()); myImageView.setImageBitmap(bm); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!