2011-05-11 67 views
0

我试图为我工作的项目创建一个RSS提要阅读器。我得到它正确地解析一切到一个textview唯一的问题是,它不会显示从解析XML文件中获得的html描述的图像。我得到一堆蓝色块而不是图像。所以我尝试使用ImgGetter方法,该方法可以将HTML标签转换为android中textview的正常web文本,但我遵循一些指南,它仍然只显示蓝色块而不是图像。ImgGetter方法不显示来自说明xml的Html图像标签

这里是我的代码:

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

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

import project.gate6.rssreader.R; 
import android.app.Activity; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.os.Environment; 
import android.text.Html; 
import android.text.Html.ImageGetter; 
import android.text.method.ScrollingMovementMethod; 
import android.widget.TextView; 

public class ShowDetails extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.details); 




     TextView detailsTitle = (TextView)findViewById(R.id.detailstitle); 
     TextView detailsDescription = (TextView)findViewById(R.id.detailsdescription); 
     TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate); 
     TextView detailsLink = (TextView)findViewById(R.id.detailslink); 


     Bundle bundle = this.getIntent().getExtras(); 



     detailsTitle.setText(bundle.getString("keyTitle")); 
     detailsDescription.setText(Html.fromHtml(bundle.getString("keyDescription"),imgGetter,null)); 
     detailsDescription.setMovementMethod(new ScrollingMovementMethod()); 
     detailsPubdate.setText(bundle.getString("keyPubdate")); 
     detailsLink.setText(bundle.getString("keyLink")); 

    } 

    static ImageGetter imgGetter = new Html.ImageGetter() { 
     @Override 
     public Drawable getDrawable(String source) { 
      HttpGet get = new HttpGet(source); 
      DefaultHttpClient client = new DefaultHttpClient(); 
      Drawable drawable = null; 
      try{ 
       HttpResponse response = client.execute(get); 
       InputStream stream = response.getEntity().getContent(); 
       FileOutputStream fileout = new FileOutputStream(new File(Environment.getExternalStorageDirectory().getAbsolutePath())); 
       int read = stream.read(); 
       while(read != -1) 
       { 
        fileout.write(read); 
        read = stream.read(); 
       } 
       fileout.flush(); 
       fileout.close(); 
       drawable = Drawable.createFromPath(Environment.getExternalStorageDirectory().getAbsolutePath()); 
       drawable.setBounds(0,0,drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 

      } 
      catch(ClientProtocolException e) 
      { 
       e.printStackTrace(); 
      } 
      catch(IOException e) 
      { 
       e.printStackTrace(); 
      } 
      return drawable; 
     } 
    }; 

} 

我想通了,网页视图效果更好这里是我做编辑我的代码:

import project.gate6.rssreader.R; 
import android.app.Activity; 
import android.os.Bundle; 

import android.webkit.WebView; 
import android.widget.TextView; 

public class ShowDetails extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.details); 

     final String mimetype = "text/html"; 
     final String encoding = "UTF-8"; 


     TextView detailsTitle = (TextView)findViewById(R.id.detailstitle); 
     WebView detailsDescription = (WebView)findViewById(R.id.detailsdescription); 
     TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate); 
     TextView detailsLink = (TextView)findViewById(R.id.detailslink); 


     Bundle bundle = this.getIntent().getExtras(); 



     detailsTitle.setText(bundle.getString("keyTitle")); 
     detailsDescription.loadDataWithBaseURL("",bundle.getString("keyDescription"), mimetype, encoding, ""); 
     detailsPubdate.setText(bundle.getString("keyPubdate")); 
     detailsLink.setText(bundle.getString("keyLink")); 

    } 

} 
+0

您正试图获取在TextView中显示的图像?这是不可能的...(好吧,至少不是简单的,它并不意味着要做...) – WarrenFaith 2011-05-11 21:18:15

+0

是的,我认为这将是可能的,因为根据文档的方法Html.fromHtml说它应该是能够读取显示在字符串中的图像。 – Novazero 2011-05-11 21:20:06

+0

嗯也许它会更好地尝试使用webview阅读的HTML和显示页面?尽管从我看过的表现来看非常糟糕。 – Novazero 2011-05-11 21:24:22

回答

0

使用网页视图代替,这完美地工作。解决方案是在我的第一篇文章。