[英]Unable to Download an Image Due to Spaces in URL
I have an application where i download images from a server and display them in a imageview. 我有一个应用程序,我从服务器下载图像,并在imageview中显示它们。 This works the majority of the time but not always.
这在大多数时间都有效,但并非总是如此。 It seems the times it doesn't work is when there are spaces in the url.
似乎它不起作用的时候是网址中有空格的时候。 The error i get is
我得到的错误是
java.lang.RuntimeException: java.io.FileNotFoundException:
java.lang.RuntimeException:java.io.FileNotFoundException:
I have tried a number of different ways to try and encode the url but have had no success upto now. 我已经尝试了许多不同的方法来尝试编码网址,但到目前为止还没有成功。
Here is the class I am using. 这是我正在使用的课程。
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);
}
Both the getImage and getBitmap have the same error. getImage和getBitmap都有相同的错误。
Here is an example of a url i am using. 这是我正在使用的网址示例。
http://kaiorize-clone1.loomarea.com/sites/default/files/imagecache/app_product_full/sites/default/files/BsfL_Logo_final_sonderfarben 300dpi.jpg http://kaiorize-clone1.loomarea.com/sites/default/files/imagecache/app_product_full/sites/default/files/BsfL_Logo_final_sonderfarben 300dpi.jpg
This was a strange one. 这是一个奇怪的。 I tried to encode the normal way but I was still getting the same error.
我尝试编码正常的方式,但我仍然得到相同的错误。 I check the URL that was being sent and the space had been changed for a "%20".
我检查了发送的URL,并且空间已经更改为“%20”。 In the end through desperation I decided to just change the space into "%20" myself instead of encoding.
最后,我绝望地决定将空间改为“%20”而不是编码。 This solved the problem.
这解决了这个问题。 Not the most elegent of solutions but it worked!
不是最优雅的解决方案,但它有效!
String url1 = json.getString("app_imagepath");
String url = url1.replace(" ", "%20");
URL encoding converts characters into a format that can be transmitted over the Internet. URL编码将字符转换为可以通过Internet传输的格式。 For example, space is encoded as %20.
例如,空格编码为%20。
You can find the entire table at http://www.w3schools.com/tags/ref_urlencode.asp . 您可以在http://www.w3schools.com/tags/ref_urlencode.asp找到整个表格。
Try this code snippet, 试试这段代码,
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.