简体   繁体   English

如何从RSS Feed中读取图片并在Android中显示?

[英]How to read picture from RSS Feed and show in android?

I try to learn RSS Feed from this tutorial . 我尝试从本教程中学习RSS Feed。 After read that tutorial I download source and test RSS Feed. 阅读该教程后,我将下载源代码并测试RSS Feed。 but it can't read picture from RSS to show in android because my RSS have no tag media:content but it use tag enclosure such as 但它无法从RSS读取图片以在android中显示,因为我的RSS没有标签media:content,但它使用标签附件,例如

<enclosure url="http://p3.s1sf.com/ns/0/ud/238/1192436/1.jpg" type="image/jpeg"/>

when I run the code from tutorial it show like this in log. 当我从教程中运行代码时,它在日志中显示如下。

06-19 08:43:57.637: D/rssfeed(785): imageAndTexts1.get(position).getImgLink() :: null :: 06-19 08:43:57.637:D / rssfeed(785):imageAndTexts1.get(position).getImgLink()::空::

So I checked XmlHandler.java in startElement function. 所以我在startElement函数中检查了XmlHandler.java

if (qName.equalsIgnoreCase("media:content"))

        {
            if (!atts.getValue("url").toString().equalsIgnoreCase("null")) {
                feedStr.setImgLink(atts.getValue("url").toString());
            } else {
                feedStr.setImgLink("");
            }
        }

I replace with 我替换为

if (qName.equalsIgnoreCase("enclosure"))

        {
            if (!atts.getValue("url").toString().equalsIgnoreCase("null")) {
                feedStr.setImgLink(atts.getValue("url").toString());
            } else {
                feedStr.setImgLink("");
            }
        }

but It show error like this 但是它显示这样的错误

06-19 08:08:09.165: E/AndroidRuntime(2661): FATAL EXCEPTION: main
06-19 08:08:09.165: E/AndroidRuntime(2661): android.os.NetworkOnMainThreadException
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at com.amit.adapter.RssReaderListAdapter.getView(RssReaderListAdapter.java:64)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.ListView.fillDown(ListView.java:674)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.ListView.fillFromTop(ListView.java:735)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.ListView.layoutChildren(ListView.java:1652)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.Choreographer.doFrame(Choreographer.java:532)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.os.Handler.handleCallback(Handler.java:725)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.os.Looper.loop(Looper.java:137)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 08:08:09.165: E/AndroidRuntime(2661):     at dalvik.system.NativeStart.main(Native Method)

I don't know how to read image from RSS both tag media:content and enclosure. 我不知道如何从RSS读取媒体标签:内容和附件。 please help me. 请帮我。

U get an url through rss feed and pass the url to this function like this ` U通过rss feed获取一个url并将该URL传递给此函数,例如`

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

` and finally u get an image then u set it into the imageview like this Imageview.setImageBitmap `最后得到一个图像,然后将其像这样设置到imageview.setImageBitmap

This is not an error relavent how you parse rss. 这并不是错误的解析rss的方式。 Error is "NetworkOnMainThreadException" so this means you are trying to get this rss on main thread, Android platform is not allowed to access network on main thread to prevent locking user's events. 错误是“ NetworkOnMainThreadException”,因此这意味着您试图在主线程上获取此rss,不允许Android平台在主线程上访问网络以防止锁定用户事件。 You must call this rss reading method on a background thread... 您必须在后台线程上调用此rss阅读方法...

new Thread(new Runnable() { 
        @Override
        public void run() {
            ReadRss();
        } 
    }).start();

You must use this downloading action on the main thread with using AsyncTask . 您必须使用AsyncTask在主线程上使用此下载操作。 See this . 看到这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM