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"));
}
}
您正試圖獲取在TextView中顯示的圖像?這是不可能的...(好吧,至少不是簡單的,它並不意味着要做...) – WarrenFaith 2011-05-11 21:18:15
是的,我認爲這將是可能的,因爲根據文檔的方法Html.fromHtml說它應該是能夠讀取顯示在字符串中的圖像。 – Novazero 2011-05-11 21:20:06
嗯也許它會更好地嘗試使用webview閱讀的HTML和顯示頁面?儘管從我看過的表現來看非常糟糕。 – Novazero 2011-05-11 21:24:22