【问题标题】:Get Image from Url Unable to Decode Stream: fileNotFoundException从 Url 获取图像无法解码流:fileNotFoundException
【发布时间】:2017-05-16 13:00:57
【问题描述】:

我知道这个问题被问了很多次,但我尝试了很多解决方案,但没有一个有效。

在 Android 上,我试图从 URL 获取图像以将其放入图像视图中。 不幸的是,我收到以下错误:

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

当我尝试从模拟器的浏览器访问此 URL 时,它可以工作。

我已经尝试过以下解决方案: Load image from url

How to get image from url website in imageview in android

how to set image from url for imageView

我的实际代码如下:

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;

    }
}

在主要活动中,我的代码是:

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

在我添加的清单中:

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

我能做什么?

【问题讨论】:

  • 您在哪个设备操作系统中测试这个??
  • 试试这个库:github.com/bumptech/glide
  • http://lorempixel.com/1920/1920/business 导致 HTTP 301 响应,将您重定向到 http://lorempixel.com/1920/1920/business/。您的setInstanceFollowRedirects(true) 应该可以处理,但我很少使用HttpURLConnection,所以我不知道那里是否有任何问题。与 Sac 一样,我建议您使用 an image-loading library(例如 Picasso)。
  • 所以我尝试了http://lorempixel.com/1920/1920/business/,但我遇到了同样的错误。我不能使用任何外部图书馆,因为这是学校练习。我认为问题在于它会在 HttpConnection 正常之前尝试 decodeStream 。我怎样才能强迫它等待?

标签: android url imageview


【解决方案1】:

这很完美:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多