来自异步任务的图像从不显示

时间:2013-06-19 21:16:35

标签: android android-asynctask android-xml

我试图实现我从这里得到的图像异步任务:

http://schimpf.es/asynctask-for-image-download/

然后我尝试在我的代码中执行它:

ImageView im1 = (ImageView) findViewById(R.id.image);
ImageDownloadTask imageD = new ImageDownloadTask(im1);
imageD.execute(e.mediumLabel);

图像永远不会加载......

我的xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/beerTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="40sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <ImageView android:id="@+id/image"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_margin="10dip"/>

    <Button
        android:id="@+id/buttonBrewery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="" />
    <Button
        android:id="@+id/buttonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="" />

    <TextView
        android:id="@+id/beerDEscriptionTitle"
        android:textStyle = "bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:text="Description"
        android:padding="5dip"
        ></TextView>
    <TextView
        android:id="@+id/beerDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="15sp"
        android:padding="5dip"

        ></TextView>

</LinearLayout>
</ScrollView>

3 个答案:

答案 0 :(得分:3)

帮自己一个忙,并使用毕加索:http://square.github.io/picasso/

答案 1 :(得分:1)

毫无意外的是没有用。永远不会设置ASyncTask中的字段“url”。在ASyncTask中更改此行:

 InputStream in = new java.net.URL(url).openStream();

实际使用在execute(String)中发送的参数...这应该有用的链接:

 InputStream in = new java.net.URL(params[0]).openStream();

答案 2 :(得分:0)

AsyncTask图像加载器类中的URL永远不会在任何地方设置,因此永远不会下载图像。在调用execute()时将其设置在构造函数或params中。我会研究使用其他一些加载图像的方法。

另外,请确保您拥有相应的互联网权限:

<uses-permission android:name="android.permission.INTERNET">