Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从internet加载的图像显示不正确?_Android_Url_Imageview - Fatal编程技术网

Android 从internet加载的图像显示不正确?

Android 从internet加载的图像显示不正确?,android,url,imageview,Android,Url,Imageview,我在一个图像视图中显示一幅图像,该图像是从互联网上获得的。问题是,如果我只看了3到4次页面,而图像显示了3到4次(每次我从互联网下载图像),那么它不会一直显示出来。我如何使它每次都显示出来 public Bitmap getDrawable(String url) throws MalformedURLException, IOException { Bitmap x; HttpURLConnection connection = (HttpURLConnect

我在一个图像视图中显示一幅图像,该图像是从互联网上获得的。问题是,如果我只看了3到4次页面,而图像显示了3到4次(每次我从互联网下载图像),那么它不会一直显示出来。我如何使它每次都显示出来

public Bitmap getDrawable(String url) throws MalformedURLException, IOException {
        Bitmap x;

        HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();

        connection.connect();
        InputStream input = connection.getInputStream();

        x = BitmapFactory.decodeStream(input);
        return x;
    }

for (int i = 0; i < imageSourceArray.length - 1; i++) {
        detailedArticleImageViewArray[i] = new ImageView(
            ArticleActivity.this);
        System.out.println(TMI + imageSourceArray[i + 1]);
        Bitmap image = getDrawable(TMI + imageSourceArray[i + 1]);
        detailedArticleImageViewArray[i].setImageBitmap(image);
        detailedArticleImageViewArray[i].setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, 250));
        detailedArticleImageViewArray[0].setPadding(5, 10, 10, 5);
                }
公共位图getDrawable(字符串url)引发畸形的异常,IOException{
位图x;
HttpURLConnection连接=(HttpURLConnection)新URL(URL).openConnection();
connection.connect();
InputStream输入=连接。getInputStream();
x=BitmapFactory.decodeStream(输入);
返回x;
}
对于(int i=0;i
我正在获取IOException缓冲的InputStream已关闭

另一个原因是解码返回null或false


提前感谢。

如果您试图从url显示图像,请使用此选项

Bitmap mbmp = BitmapFactory.decodeStream(new java.net.URL("urlname").openStream());
Imageview_ref.setImageBitmap(mbmp);
我认为无需每次下载即可显示图像。

使用此类

static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                int b = read();
                if (b < 0) {
                    break; // we reached EOF
                } else {
                    bytesSkipped = 1; // we read one byte
                }
            }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}
静态类FlushedInputStream扩展FilterInputStream{
公共FlushedInputStream(InputStream InputStream){
超级(输入流);
}
@凌驾
公共长跳过(长n)引发IOException{
长的总重量=0升;
while(totalBytesSkipped
这不是问题所在,当我重新打开页面时,出现“IOException BufferedInput Stream is closed”错误,显示图像…位图mbmp=BitmapFactory.decodeStream(新的java.net.URL(“urlname”).openStream();在这一行中,我得到了IOException我已经打印了图像url它确定不为空吗?