从 URL 获取图像无法解码流:fileNotFoundException [英] Get Image from Url Unable to Decode Stream: fileNotFoundException

查看:27
本文介绍了从 URL 获取图像无法解码流:fileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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)

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

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

我已经尝试过以下解决方案:从url加载图片

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

如何从 url 网站获取图片在android中的imageview中

如何从 url 为 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;

    }
}

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

In the main activity my code is :

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

在我添加的清单中:

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

我能做什么?

推荐答案

这很完美:

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

这篇关于从 URL 获取图像无法解码流:fileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆