我尝试在以下网址下载图像:
http://upload.tapcrowd.com//cache/ /_cp_100_100_stand_filière_300x212.jpg
正如您在浏览器中看到的那样,这显示了一个图像,但在我的应用程序中,我得到了 FileNotFoundException。
但是,如果我将图像的 url 从“è”更改为“e”。我可以成功地将它下载到我的应用程序中。然而,这只是一个临时解决方案,因为它需要能够下载带有 unicode 符号的图像。
我怎样才能做到这一点?
用于下载图像的方法:
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f, maxheight, maxwidth);
对我有用的结果代码:
Bitmap bitmap = null;
int slashIndex = url.lastIndexOf('/');
String filename = url.substring(slashIndex + 1);
filename = URLEncoder.encode(filename, "UTF-8");
url = url.subSequence(0, slashIndex + 1) + filename;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f, maxheight, maxwidth);