AsyncTask下载图像错误

时间:2012-09-06 15:47:03

标签: android android-asynctask

我使用Google Chart API在我的应用中构建图表。

在我的情况下,我需要在每张图表上下载图像。我使用AsyncTask。

要监控下载过程,请使用“进度”对话框。但是在这类AsyncTask中出现了错误。

执行AsyncTask:

new loadChart(ChartAct.this,img).execute();

和AsyncTask的代码:

public class loadChart extends AsyncTask<Void, Void, Void> {
    private ImageView img;
    private Context con;
    private ProgressDialog dialog;

    public loadChart(Context context, ImageView img1) {
        this.img = img1;
        this.con = context;
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(con, "Connecting:",
                "Loading. Please wait...", true);
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        final Bitmap bitmap = DownloadImage();
        img.setImageBitmap(bitmap);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        super.onPostExecute(result);
    }

    private static InputStream OpenHttpConnection(String urlString)
            throws IOException {

        Log.d("palval", "OpenHttpConnection");
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();

            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }

            String res = Integer.toString(response);
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    public static Bitmap DownloadImage() {
        Log.d("palval", "DownloadImage");
        Bitmap bitmap = null;
        InputStream in = null;
        try {

            in = OpenHttpConnection("https://chart.googleapis.com/chart?chs=440x220&chd=t:60,40&cht=p3&chl=Hello|World");
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }

}

错误:

09-06 18:35:35.574: E/AndroidRuntime(2502): FATAL EXCEPTION: AsyncTask #1
09-06 18:35:35.574: E/AndroidRuntime(2502): java.lang.RuntimeException: An error occured while executing doInBackground()
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.lang.Thread.run(Thread.java:864)
09-06 18:35:35.574: E/AndroidRuntime(2502): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4132)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:723)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.view.View.requestLayout(View.java:12957)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.widget.ImageView.setImageDrawable(ImageView.java:362)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.widget.ImageView.setImageBitmap(ImageView.java:377)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at com.example.headache.loadChart.doInBackground(loadChart.java:37)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at com.example.headache.loadChart.doInBackground(loadChart.java:1)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-06 18:35:35.574: E/AndroidRuntime(2502):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-06 18:35:35.574: E/AndroidRuntime(2502):     ... 5 more
09-06 18:35:36.055: E/WindowManager(2502): Activity com.example.headache.ChartAct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd3b28 that was originally added here
09-06 18:35:36.055: E/WindowManager(2502): android.view.WindowLeaked: Activity com.example.headache.ChartAct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40dd3b28 that was originally added here
09-06 18:35:36.055: E/WindowManager(2502):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:352)
09-06 18:35:36.055: E/WindowManager(2502):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:373)
09-06 18:35:36.055: E/WindowManager(2502):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:321)
09-06 18:35:36.055: E/WindowManager(2502):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
09-06 18:35:36.055: E/WindowManager(2502):  at android.view.Window$LocalWindowManager.addView(Window.java:541)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.Dialog.show(Dialog.java:301)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.ProgressDialog.show(ProgressDialog.java:116)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.ProgressDialog.show(ProgressDialog.java:99)
09-06 18:35:36.055: E/WindowManager(2502):  at com.example.headache.loadChart.onPreExecute(loadChart.java:29)
09-06 18:35:36.055: E/WindowManager(2502):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
09-06 18:35:36.055: E/WindowManager(2502):  at android.os.AsyncTask.execute(AsyncTask.java:511)
09-06 18:35:36.055: E/WindowManager(2502):  at com.example.headache.ChartAct.onCreate(ChartAct.java:31)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.Activity.performCreate(Activity.java:4531)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
09-06 18:35:36.055: E/WindowManager(2502):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 18:35:36.055: E/WindowManager(2502):  at android.os.Looper.loop(Looper.java:154)
09-06 18:35:36.055: E/WindowManager(2502):  at android.app.ActivityThread.main(ActivityThread.java:4945)
09-06 18:35:36.055: E/WindowManager(2502):  at java.lang.reflect.Method.invokeNative(Native Method)
09-06 18:35:36.055: E/WindowManager(2502):  at java.lang.reflect.Method.invoke(Method.java:511)
09-06 18:35:36.055: E/WindowManager(2502):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-06 18:35:36.055: E/WindowManager(2502):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-06 18:35:36.055: E/WindowManager(2502):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您无法在doInBackground()中设置图片,使其返回位图并在onPostExecute()

中设置
public class loadChart extends AsyncTask<Void, Void, Bitmap> {
    private ImageView img;
    private Context con;
    private ProgressDialog dialog;

    public loadChart(Context context, ImageView img1) {
        this.img = img1;
        this.con = context;
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(con, "Connecting:",
                "Loading. Please wait...", true);
        super.onPreExecute();
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        final Bitmap bitmap = DownloadImage();
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        dialog.dismiss();
        img.setImageBitmap(result);
    }

答案 1 :(得分:0)

您无法从doInBackground()访问用户界面,这就是您使用img.setImageBitmap(bitmap);设置位图所做的工作。如果您在使用runOnUiThread()时仍在进行后台活动时需要执行此操作,请将您的UI访问代码移至onPostExecute(),或者将对象存储在对象中,但分配给onProgressUpdate()中的UI元素。一般来看AsyncTask documentation

答案 2 :(得分:0)

您无法从doInBackground()修改UI元素。

尝试使用Activity的runOnUIThread()方法或将更改UI的方法放入onPostExecute()方法。

阅读this answer了解详情。