android– 由于URL中的空格而无法下载图像

android– 由于URL中的空格而无法下载图像,第1张

概述我有一个应用程序,我从服务器下载图像,并在imageview中显示它们.这在大多数时间都有效,但并非总是如此.似乎它不起作用的时候是网址中有空格的时候.我得到的错误是java.lang.RuntimeException:java.io.FileNotFoundException:我已经尝试了许多不同的方法来尝试编码网址,但到

我有一个应用程序,我从服务器下载图像,并在imagevIEw中显示它们.这在大多数时间都有效,但并非总是如此.似乎它不起作用的时候是网址中有空格的时候.我得到的错误是

java.lang.RuntimeException: java.io.fileNotFoundException:

我已经尝试了许多不同的方法来尝试编码网址,但到目前为止还没有成功.

这是我正在使用的课程.

private URL url;Bitmap bitmap;public ImageDownloader() {}//constructorpublic Drawable getimage(String urlString) throws IOException{    Log.i("url", urlString);    url = new URL(urlString);    inputStream is = url.openStream();    Bitmap bitmap = BitmapFactory.decodeStream(new FlushedinputStream(is));    Drawable image = new BitmapDrawable(bitmap);    return image; }//getimagestatic 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 bytee = read();                  if (bytee < 0) {                      break;  // we reached EOF                  } else {                      bytesSkipped = 1; // we read one byte                  }           }            totalBytesSkipped += bytesSkipped;        }        return totalBytesSkipped;    }}public Bitmap getBitmap(String urlString) {    try {        //String s = Uri.encode(urlString);        url = new URL(urlString);    } catch (MalformedURLException e1) {        // Todo auto-generated catch block        e1.printstacktrace();    }    httpURLConnection conn = null;    try {        conn = (httpURLConnection) url.openConnection();           conn.setRequestProperty("User-agent", "Mozilla/4.0");           conn.connect();    } catch (IOException e1) {        // Todo auto-generated catch block        e1.printstacktrace();    }    inputStream in = null;    try {        in = conn.getinputStream();    } catch (IOException e) {        // Todo auto-generated catch block        e.printstacktrace();    }    return bitmap = BitmapFactory.decodeStream(in);}

getimage和getBitmap都有相同的错误.

这是我正在使用的网址示例.

http://kaiorize-clone1.loomarea.com/sites/default/files/imagecache/app_product_full/sites/default/files/BsfL_Logo_final_sonderfarben 300dpi.jpg

解决方法:

这是一个奇怪的.我尝试编码正常的方式,但我仍然得到相同的错误.我检查了发送的URL,并且空间已经更改为“”.最后,我绝望地决定将空间改为“”而不是编码.这解决了这个问题.不是最优雅的解决方案,但它有效!

        String url1 = Json.getString("app_imagepath");        String url = url1.replace(" ", "%20");
总结

以上是内存溢出为你收集整理的android – 由于URL中的空格而无法下载图像全部内容,希望文章能够帮你解决android – 由于URL中的空格而无法下载图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1114231.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存