我想从URL获取一个位图,我的代码是:
public Bitmap getBitmapFromUrl(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
//testBitMap = myBitmap;
Log.d("myLogs", "return Bitmap");
return myBitmap;
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
return null;
}
}
但是当我把网址添加到西里尔语http://cl228041.tmweb.ru/sites/default/files/бмв_0.jpg
时我无法获取位图
我试试:
URLEncoder.encode(yourString, HTTP.UTF-8);
但它不起作用。
答案 0 :(得分:2)
试试这个
url = url.replaceAll(" ", "%20");
URL imageUrl = new URL(new URI(url).toASCIIString());
HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();