0

我想从 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;
    }
}

但是,当我将 url 与西里尔字母放在一起时无法获得位图时,http://cl228041.tmweb.ru/sites/default/files/бмв_0.jpg 我尝试:

URLEncoder.encode(yourString, HTTP.UTF-8);

但它不起作用。

4

1 回答 1

2

尝试这个

        url = url.replaceAll(" ", "%20");
        URL imageUrl = new URL(new URI(url).toASCIIString());
        HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
于 2014-01-08T13:15:07.020 回答