使用AsyncTask下载许多图像并将它们发布到ImageView

使用AsyncTask下载许多图像并将它们发布到ImageView,第1张

概述我坐着试着用Android做一些练习.我今天的观点是制作简单的应用程序,它将下载数据(来自URL的图像)并在布局中的ImageView控件中显示它们.我在网上看到了一些例子,并完成了我的应用程序.一切似乎都没问题,但当我按下按钮时,我开始工作,但后来显示错误:NULLPOINTER9error读取文件).这

我坐着试着用Android做一些练习.我今天的观点是制作简单的应用程序,它将下载数据(来自URL的图像)并在布局中的ImageVIEw控件中显示它们.我在网上看到了一些例子,并完成了我的应用程序.一切似乎都没问题,但当我按下按钮时,我开始工作,但后来显示错误:NulL POINTER 9error读取文件).这是我的代码:

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" en@R_403_5563@="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>

我在代码ArrayList中看到了Drawable列表.代码中没有错误.只有那个NulL POINTER.

解决方法:

将您的异步任务更改为

 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下载许多图像并将它们发布到ImageView全部内容,希望文章能够帮你解决使用AsyncTask下载许多图像并将它们发布到ImageView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1107039.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存