使用AsyncTask下载多张图片并将它们发送到ImageView。

3

我正试图在Android上进行一些练习。我今天的目标是创建一个简单的应用程序,它将下载数据(从URL中获取的图像)并在布局中的ImageView控件中显示它们。我在网上看到了一些示例,并完成了我的应用程序。一切似乎都很好,但当我点击按钮时,它开始工作,但随后失败,并显示错误:空指针异常(9error reading file)。以下是我的代码:

package com.example.htmlcontent;

import java.io.BufferedInputStream;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


    public class MainActivity extends Activity {
        private ImageView mImageView;
        private ImageView mImageView2;
        public Button button;
        public static ArrayList<Drawable> drawable;

        public static String[] URLs = {"http://zitterman.com/wp-content/uploads/2013/07/19194927_1371972212.jpg","http://i.imgur.com/CQzlM.jpg"};

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            mImageView = (ImageView) findViewById(R.id.test_image);
            mImageView2 = (ImageView) findViewById(R.id.test_image2);
            button = (Button) findViewById(R.id.download1);

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    new DownloadImage().execute();
                }
            });
        }


        /**
         * Simple functin to set a Drawable to the image View
         * @param drawable
         */
        @SuppressWarnings("deprecation")
        private void setImage()
        {
            if(drawable.get(0) == null)
            {
                System.out.println("DRAWABLE JEST NULL");
            }
            mImageView.setBackgroundDrawable(drawable.get(0));
            mImageView2.setBackgroundDrawable(drawable.get(1));
        }

        public class DownloadImage extends AsyncTask<Void, Void, Void> {



            /**
             * Called after the image has been downloaded
             * -> this calls a function on the main thread again
             */
            protected void onPostExecute(Drawable image)
            {
                setImage();
            }
            protected void onPreExecute()
            {
                Log.i("333333", "Uruchamiam WATEK SCIAGANIA ASYNCTASKIEM PLIKU Z NETA");
            }

            @Override
            protected Void doInBackground(Void... params) {

                downloadImage();
                return null;
            }
            /**
             * Actually download the Image from the _url
             * @param _url
             * @return
             */
            @SuppressWarnings("deprecation")
            private void downloadImage()
            {
                //Prepare to download image

                URL url;        

                InputStream in;
                BufferedInputStream buf;

                //BufferedInputStream buf;
                for(int i = 0; i<URLs.length; i++)
                {
                    try {
                    url = new URL(URLs[i]);
                    in = url.openStream();

                    // Read the inputstream 
                    buf = new BufferedInputStream(in);

                    // Convert the BufferedInputStream to a Bitmap
                    Bitmap bMap = BitmapFactory.decodeStream(buf);
                    if (in != null) {
                        in.close();
                    }
                    if (buf != null) {
                        buf.close();
                    }

                     drawable.add(new BitmapDrawable(bMap));

                } catch (Exception e) {
                    Log.e("Error reading file", e.toString());
                }

                }

            }


        }
    }

我的XML文件布局如下:

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

    <Button
        android:id="@+id/download1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

<TextView
    android:layout_width="102dp"
    android:layout_height="wrap_content"
    android:text="hello" />

    <ImageView
        android:id="@+id/test_image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/ic_launcher" />
    <ImageView
        android:id="@+id/test_image2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_launcher" />

</LinearLayout>

我已经按照代码中的方式创建了一个Drawable列表ArrayList。代码没有错误,但是出现了空指针异常。


请发布logcat日志。 - Ahmed Ekri
在你的catch块中,执行e.printStackTrace()并将logcat的输出发布出来。 - Amulya Khare
我建议您使用https://github.com/nostra13/Android-Universal-Image-Loader,它非常易于使用,并且几乎可以为您处理所有事情。 - JustDoingIt
3个回答

4
我认为这是因为您忘记初始化可绘制对象。请改为以下内容:
public static ArrayList<Drawable> drawable = new ArrayList<Drawable>();

由于您的AsyncTask是<Void,Void,Void>,因此您的post execute应如下所示:

    @Override
    protected void onPostExecute(Void aVoid) {
        setImage();
    }

通用类型<A,B,C>对应于不同方法的参数和返回类型。您可以在此处阅读更多内容:https://dev59.com/m2025IYBdhLWcg3wW0ux#6053673

(为了完整起见)您还需要在AndroidManifest.xml中添加互联网权限(就在<application...之前):

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

是的,我忘记了那个;) - Darek

1

将您的异步任务更改为:

 public class DownloadImage extends AsyncTask<Void, Void,  ArrayList<Drawable>> {



        /**
         * Called after the image has been downloaded
         * -> this calls a function on the main thread again
         */
        protected void onPostExecute( ArrayList<Drawable> drawable)
        {
            setImage(drawable);
        }
        protected void onPreExecute()
        {
            Log.i("333333", "Uruchamiam WATEK SCIAGANIA ASYNCTASKIEM PLIKU Z NETA");
        }

        @Override
        protected  ArrayList<Drawable> doInBackground(Void... params) {

            downloadImage();
            return drawable;
        }




 private void setImage(ArrayList<Drawable> drawable)
    {
        if(drawable.get(0) == null)
        {
            System.out.println("DRAWABLE JEST NULL");
        }
        mImageView.setBackgroundDrawable(drawable.get(0));
        mImageView2.setBackgroundDrawable(drawable.get(1));
    }

好的,谢谢,它起作用了!但是你能告诉我为什么应该这样吗?我理解我静态地将数组发送到AsyncTask,所以第一个参数是Void。没有更新,所以它是Void。为什么第三个参数不应该是Void?在doInBackground中...如果我有静态数组并且它是从另一个函数(downloadImage)完成的,为什么还需要返回呢?为什么setImage函数应该具有此参数,如果数组是静态的? - Darek

0

好的,我按照你说的进行了更正。是的,我忘记在开始时初始化这个ArrayList了,但你比我更快;)所以

public class MainActivity extends Activity {
        private ImageView mImageView;
        private ImageView mImageView2;
        public Button button;
        public static ArrayList<Drawable> drawable = new ArrayList<Drawable>();

        public static String[] URLs = {"http://zitterman.com/wp-content/uploads/2013/07/19194927_1371972212.jpg","http://i.imgur.com/CQzlM.jpg"};

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            mImageView = (ImageView) findViewById(R.id.test_image);
            mImageView2 = (ImageView) findViewById(R.id.test_image2);
            button = (Button) findViewById(R.id.download1);

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    new DownloadImage().execute();
                }
            });
        }


        /**
         * Simple functin to set a Drawable to the image View
         * @param drawable
         */
        @SuppressWarnings("deprecation")
        private void setImage( ArrayList<Drawable> drawable)
        {
            if(drawable.get(0) == null)
            {
                System.out.println("DRAWABLE JEST NULL");
            }
            mImageView.setBackgroundDrawable(drawable.get(0));
            mImageView2.setBackgroundDrawable(drawable.get(1));
        }

        public class DownloadImage extends AsyncTask<Void, Void, ArrayList<Drawable>> {

            /**
             * Called after the image has been downloaded
             * -> this calls a function on the main thread again
             */
            protected void onPostExecute( ArrayList<Drawable> drawable)
            {
                setImage(drawable);
            }
            protected void onPreExecute()
            {
                Log.i("333333", "Uruchamiam WATEK SCIAGANIA ASYNCTASKIEM PLIKU Z NETA");
            }

            @Override
            protected ArrayList<Drawable> doInBackground(Void... params) {

                downloadImage();
                return null;
            }
            /**
             * Actually download the Image from the _url
             * @param _url
             * @return
             */
            @SuppressWarnings("deprecation")
            private void downloadImage()
            {
                //Prepare to download image

                URL url;        

                InputStream in;
                BufferedInputStream buf;

                //BufferedInputStream buf;
                for(int i = 0; i<URLs.length; i++)
                {
                    try {
                    url = new URL(URLs[i]);
                    in = url.openStream();

                    // Read the inputstream 
                    buf = new BufferedInputStream(in);

                    // Convert the BufferedInputStream to a Bitmap
                    Bitmap bMap = BitmapFactory.decodeStream(buf);
                    if (in != null) {
                        in.close();
                    }
                    if (buf != null) {
                        buf.close();
                    }

                     drawable.add(new BitmapDrawable(bMap));

                } catch (Exception e) {
                    Log.e("Error reading file", e.toString());
                }

                }

            }


        }
    }

现在尝试下载图片时它会崩溃。我不太明白为什么我应该将ArrayList作为第三个参数...?如果我声明了静态数组来存储我的图像,为什么要这样做?在OnPostExecute中,它只需要调用函数来完成其余的工作。


你应该编辑你的原始问题或发布一个新的问题。回答问题会误导他人并可能导致你被踩。 - Amulya Khare
我已经更新了我的答案。现在应该一切正常了。希望你已经在你的Android清单中添加了互联网权限。 - Amulya Khare
在doInBackground方法中,您正在返回null,请将其更改为返回drawable,因为无论您在doInBackground中返回什么,它都会进入postexecute。是的,您可以在async task中不给参数使用它,因为您的数组是静态的,但是如果您像您正在给出的那样给出参数,请不要忘记返回doinbackground即drawable的结果。 - Smogger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接