Android - imagegetter java.io.FileNotFoundException

时间:2015-08-10 16:19:50

标签: android

我有html文本,我正在将其解析为TextView。文字有图像所以我找到了imagegetter

imagegetter的代码:

 spanned = Html.fromHtml(content, new Html.ImageGetter() {

            @Override
            public Drawable getDrawable(String source) {

                /*Toast.makeText(getApplicationContext(), source,
                        Toast.LENGTH_LONG).show();*/

                Drawable drawable = null;
                if (source.startsWith("http")) {
                    // load from internet

                    URL sourceURL;
                    try {
                        sourceURL = new URL(source);
                        URLConnection urlConnection = sourceURL.openConnection();
                        urlConnection.connect();
                        InputStream inputStream = urlConnection.getInputStream();
                        BufferedInputStream bufferedInputStream =
                                new BufferedInputStream(inputStream);
                        Bitmap bm = BitmapFactory.decodeStream(bufferedInputStream);

                        // convert Bitmap to Drawable
                        drawable = new BitmapDrawable(resources, bm);

                        Display display = windowManager.getDefaultDisplay();
                        Point size = new Point();
                        display.getSize(size);
                        int width = size.x;
                        int height = size.y;

                        int k = width / bm.getWidth();

                        if(k > 2) { k = 2; }

                        drawable.setBounds(0, 0,
                                bm.getWidth() * k,
                                bm.getHeight() * k);

                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                } else {
                    // load from local drawable
                }

                return drawable;
            }

        }, null);

它工作正常,但是当它有URL时:

  

http://www.svetandroida.cz/media/2016/06/android-pro-začátečníky-velikost-SIM.jpg

它得到

  

java.io.FileNotFoundException:   http://www.svetandroida.cz/media/2016/06/android-pro-začátečníky-velikost-SIM.jpg

我该如何解决?

1 个答案:

答案 0 :(得分:0)

当服务器返回指示页面不存在的错误时,将返回

FileNotFoundException。虽然在您的情况下页面确实存在,但我认为您因为网址中的重音字符而导致错误,即čáí

您需要对字符进行URL编码,以便您的网址变为:

http://www.svetandroida.cz/media/2016/06/android-pro-za%C4%8D%C3%A1te%C4%8Dn%C3%ADky-velikost-SIM.jpg