无法从网址获取图片

时间:2015-03-15 16:27:56

标签: java android

我需要从网址获取图片并将其设置为列表视图

这是我的主要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

<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" />

错误在image.setImageResource(R.drawable.cover)显示为NULLPOINTEREXCEPTION

以下是代码:

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

1 个答案:

答案 0 :(得分:0)

您无法使用默认适配器实现此目的。您需要为listview创建自定义适配器。

在适配器的getView方法中,您需要膨胀列表项布局,然后必须将图像绑定到图像视图。

您可以参考this例如