私が取り組んでいるプロジェクトの RSS フィード リーダーを作成しようとしています。すべてをテキストビューに正しく解析するようにしましたが、唯一の問題は、html の説明を取得する解析済み xml ファイルから取得した画像が表示されないことです。画像の代わりに青いブロックがたくさん表示されます。そこで、Html.toHtml メソッドで機能する ImgGetter メソッドを使用してみました。このメソッドは、HTML タグを Android のテキストビューの通常の Web テキストに変換できますが、いくつかのガイドに従っても、画像ではなく青いブロックしか表示されません。
これが私のコードです:
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;
}
};
}
ここで、コードを編集するために行ったことは、webview の方が適切であることがわかりました。
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"));
}
}