简体   繁体   English

无法从网址获取图片

[英]Unable to get image from url

I need to get images from url and set it in a listview 我需要从url获取图像并将其设置在列表视图中

Here is my main xml file: 这是我的主要xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >

<ScrollView
    android:id="@+id/scrollFeed"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="40sp"
            android:background="#d2d2d2"
            android:clickable="true"
            android:gravity="center"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:text="^\nRefresh" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="15000sp" >
        </ListView>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="100" 
            >

            <TextView
                android:id="@+id/nextPosts"
                android:layout_width="match_parent"
                android:layout_height="40sp"
                android:background="#d2d2d2"
                android:clickable="true"
                android:gravity="center"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:text="next >>"
                android:layout_weight="50" />

            <TextView
                android:id="@+id/previousPosts"
                android:layout_width="match_parent"
                android:layout_height="40sp"
                android:background="#d2d2d2"
                android:clickable="true"
                android:gravity="center"
                android:layout_weight="50"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:text=" previous" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

list_item.xml list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#abc0e3"
    android:gravity="right"
    android:text="TextView"
    android:textColor="#b1b1b1" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:autoLink="web"
    android:linksClickable="true"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:text="TextView" />

The error appears at image.setImageResource(R.drawable.cover) as NULLPOINTEREXCEPTION 错误出现在image.setImageResource(R.drawable.cover)NULLPOINTEREXCEPTION

Here's the code: 这是代码:

if (c.has("picture") == true) {
    try {
        Log.d("IT HAS PICTURE! TADA!!", "Image Found");
        imgUrl = c.getString("picture");
//      Thread thread = new Thread(){
//      public void run(){
        System.out.println("Thread Running");
        NewsFeed.this.runOnUiThread(new Runnable(){
            @Override 
            public void run() { 
            //Your code to run in GUI thread here 
            URL newUrl = null;
            try {
                newUrl = new URL(imgUrl);
            } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            myBitmap = getBitmapFromUrl(newUrl);
            image.setImageBitmap(myBitmap);
            }
        });
//    }
//  };
    thread.start();
    } catch (Exception e) {
    Log.d("ERROR LOADING IMAGE","Why you do this, InputStream? Why?");
    }
}else{
    NewsFeed.this.runOnUiThread(new Runnable(){
    @Override 
    public void run() { 
        //Your code to run in GUI thread here 
        image.setImageResource(R.drawable.cover);
    }
    });
    }

You can't achieve it with a default adapter. 您无法使用默认适配器来实现。 You need to create a custom adapter for listview. 您需要为listview创建一个自定义适配器。

In getView method of adapter you need to inflate the list item layout and then have to bind the image to image view. 在适配器的getView方法中,您需要膨胀列表项的布局,然后必须将图像绑定到图像视图。

You can refer this for example 你可以参考这个例子

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

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