尝试将图像从url加载到TextView中

时间:2013-07-15 02:25:21

标签: android image 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,检索数据,创建位图并将其转换为drawable,但是在返回drawable之前,它会跳转到返回异常部分的空行而没有任何异常。只是不知道问题是什么。


管理以使其发挥作用。只是在try附近重新组织代码并捕获它在所有方法中只有一个返回derective。并添加了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;

1 个答案:

答案 0 :(得分: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

enter image description here