我正在尝试从服务器下载图像。很少有人下载,也很少有人造成问题。我不知道为什么。
我已在同一位置下载并向用户显示图像。这是可以下载的文件。
当我尝试下载以下图像时。 此图片可以在浏览器中打开,但无法在 Android 中下载
Its give exception
java.io.FileNotFoundException: http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester United_M_785.jpg at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521) at src.com.mongreldog.appsupport.Utils.downloadImage(Utils.java:77) at src.com.mongreldog.ViewFullCompAndCommentActivity$3.performInBackground(ViewFullCompAndCommentActivity.java:607) at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:44) at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:1) at android.os.AsyncTask$2.call(AsyncTask.java:185)
这是我的代码。
public static Bitmap downloadImage(String imageURLStr) {
Bitmap bitmap = null;
InputStream in = null;
try {
URL url = new URL(imageURLStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (SocketTimeoutException e) {
bitmap = null;
e.printStackTrace();
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
bitmap = null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
bitmap = null;
}
return bitmap;
}
最佳答案
使用 URL Encoder 对图像 URL
进行编码,您可以在日志报告中看到 URL
有空格。
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = URLEncoder.encode(imageURLStr, "utf-8");
//... rest of your code.
}
<小时/>
编辑:正如您报告的问题“+”而不是 %20
你可以使用
public static Bitmap downloadImage(String imageURLStr) {
imageURLStr = imageURLStr.replaceAll(" ", "%20");
//... rest of your code.
}
用于来源检查here
关于java - 为什么在服务器上的android中找不到图像文件但在浏览器中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008848/