Android 由于URL中有空格,无法下载图像

Android 由于URL中有空格,无法下载图像,android,Android,我有一个从服务器下载图像并在imageview中显示的应用程序。这在大多数情况下有效,但并不总是有效。当url中有空格时,它似乎不起作用。我得到的错误是 java.lang.RuntimeException:java.io.FileNotFoundException: 我已经尝试了许多不同的方法来尝试和编码的网址,但没有成功到现在为止 这是我正在使用的类 private URL url; Bitmap bitmap; public ImageDownloader() { }//constru

我有一个从服务器下载图像并在imageview中显示的应用程序。这在大多数情况下有效,但并不总是有效。当url中有空格时,它似乎不起作用。我得到的错误是

java.lang.RuntimeException:java.io.FileNotFoundException:

我已经尝试了许多不同的方法来尝试和编码的网址,但没有成功到现在为止

这是我正在使用的类

private URL url;
Bitmap bitmap;

public ImageDownloader() {

}//constructor


public 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;

 }//getImage

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 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);



}
私有URL;
位图;
公共图像下载程序(){
}//建造师
公共可绘制getImage(字符串urlString)引发IOException{
Log.i(“url”,urlString);
url=新url(url字符串);
InputStream=url.openStream();
位图位图=BitmapFactory.decodeStream(新的FlushedInputStream(is));
可绘制图像=新的位图可绘制(位图);
返回图像;
}//获取图像
静态类FlushedInputStream扩展FilterInputStream{
公共FlushedInputStream(InputStream InputStream){
超级(输入流);
}
@凌驾
公共长跳过(长n)引发IOException{
长的总重量=0升;
while(totalBytesSkipped
getImage和getBitmap都有相同的错误

下面是一个我正在使用的url示例

private URL url;
Bitmap bitmap;

public ImageDownloader() {

}//constructor


public 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;

 }//getImage

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 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);



}

URL编码将字符转换为可通过Internet传输的格式。例如,空间编码为%20

您可以在中找到整个表。

尝试此代码段

String songThumbUrl = "Image URL";
        try {
        URL thumbURL = new URL(songThumbUrl);
        try {
            URLConnection urlConnection = thumbURL.openConnection();
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
            songThumbImageView.setImageBitmap(bitmap);

            bufferedInputStream.close();
            inputStream.close();

        } catch (IOException ioe) {
            System.out.println("My Exception :" + ioe);
            ioe.printStackTrace();
        }
    } catch (MalformedURLException mue) {
        System.out.println("My Exception :" + mue);
        mue.printStackTrace();
    }   

这是一个奇怪的问题。我试着用正常的方式编码,但还是出现了同样的错误。我检查正在发送的URL,并且空间已更改为“%20”。最后,由于绝望,我决定自己将空格改为“%20”,而不是编码。这就解决了问题。虽然不是最优雅的解决方案,但它确实奏效了

        String url1 = json.getString("app_imagepath");
        String url = url1.replace(" ", "%20");

这应该行得通。在浏览器中用%20而不是空格键入url,然后看看会发生什么。jiduvah,编码后,url中应该没有更多的空格。这与我的getBitmap()方法非常相似,它也有相同的问题?我猜投反对票的人没有测试代码