0

尝试将图像从 url 加载到 TextView 中,除了一件事,一切都很好。当我试图从我的 ImageGetter 返回结果时,它会跳转到代码海峡的异常部分以返回跳过上面所有代码的行,并且根本没有任何异常。

这是代码。

package net.cosplace.tabun;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Spanned;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void TabunLogin(View v)
    {
        TextView txt = (TextView) findViewById(R.id.textView1);
        AsyncTask<String, Void, Spanned> r = new RetreiveFeedTask().execute();

    }

class RetreiveFeedTask extends AsyncTask<String, Void, Spanned> {
        private Exception exception;

        protected Spanned doInBackground(String... urls) {
            try 
            {
                String inputLine = "";
                String buf = "";
                try
                {
                    URL yahoo = new URL("http://www.cosplace.net/photo.php");
                    URLConnection connection = yahoo.openConnection();
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                    while ((inputLine = in.readLine()) != null)
                        buf += inputLine;

                    in.close();
                }
                catch(Exception e)
                {
                    buf = "fail";
                }

                ImageGetter imgGetter = new ImageGetter(){
                    @Override
                    public Drawable getDrawable(String source){
                           try
                           {
                                source = "http://www.cosplace.net/"+source;
                                URL url = new URL(source);

                                InputStream is = url.openStream();
                                Bitmap b = BitmapFactory.decodeStream(is);
                                Drawable d = new BitmapDrawable(getResources(),b);
                                return d;
                           }
                           catch (Exception e)
                           {
                                System.out.println("Exc="+e);
                                return null;
                           }                    
                    }
                };
                Spanned s = Html.fromHtml(buf,imgGetter, null);

                return s;
            }
            catch (Exception e)
            {
                this.exception = e;
                return null;
            }

        }

        protected void onPostExecute(Spanned result) {
            // TODO: check this.exception 
            // TODO: do something with the feed
            TextView txt = (TextView) findViewById(R.id.textView1);
            txt.setText(result);
        }
    }
}

因此,当它转到 ImageGetter 时,它成功连接到 url、检索数据、创建位图并将其转换为可绘制对象,但在返回可绘制对象之前,它会跳转到返回异常部分的空行,根本没有任何异常。只是不知道可能是什么问题。


设法让它工作。只需在 try 附近重新组织代码并发现它在所有方法中都只有一个返回指令。并添加了 setBounds。

            Drawable d = null;
               try
               {
                    source = "http://www.cosplace.net/"+source;
                    URL url = new URL(source);
                    InputStream is = url.openStream();
                    Bitmap b = BitmapFactory.decodeStream(is);
                    d = new BitmapDrawable(getResources(),b);
                    d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
               }
               catch (Exception e)
               {
                    System.out.println("Exc="+e);
               }
               return d;
4

1 回答 1

0

您在ImageGetter中获取文件未找到异常 catch

java.io.FileNotFoundException: http://www.cosplace.net/http://itodama.net/images/banners/baner4.gif

访问从此行生成的链接

source = "http://www.cosplace.net/"+source;

在浏览器中。您将在浏览器中收到此错误

Error 404. File not found

在此处输入图像描述

于 2013-07-15T03:16:43.617 回答