ImgGetter 方法不显示描述 xml 标签中的 Html 图像

发布于 2024-11-07 07:28:12 字数 4398 浏览 1 评论 0原文

我正在尝试为我正在从事的项目创建一个 RSS 提要阅读器。我让它将所有内容正确解析为文本视图,唯一的问题是它不会显示我从解析的 xml 文件获取 html 描述的图像。我得到一堆蓝色块而不是图像。因此,我尝试使用适用于 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;
        }
    };

}

我发现网络视图效果更好,这是我编辑代码的方法:

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"));

    }

}

Im trying to create a RSS feed reader for a project that I working on. I got it to parse everything correctly into a textview the only problem is that it won't display the images that I get from the parsed xml file that gets the html description. I get a bunch of blue blocks instead of the images. So I tried using the ImgGetter method that works for the Html.toHtml method that can convert html tags into normal web text for a textview in the android, though I followed some guides and it still only displays the blue blocks instead of the image.

Here is my code:

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;
        }
    };

}

I figured out that a webview works better here is what I did to edit my code:

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"));

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷鸟归林 2024-11-14 07:28:12

相反,使用了 webview,效果非常好。解决方案在我的第一篇文章下。

Used a webview instead, which worked perfectly. The solution is under my first post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文