ssummeraw 2013-04-08 08:55 采纳率: 10%
浏览 2893
已采纳

android.view.ViewRoot$CalledFromWrongThreadException 异常

我想从服务器中下载图片后并显示出来,当进行下载时,会显示一个进度对话框。使用一个 asynctask 类。运用下面的代码:

    private void startDownload() {

    new DownloadFileAsync().execute(imageUrl);
    image.setImageBitmap(bitmap);
}
 @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                dialog = new ProgressDialog(this);
                dialog.setTitle("Loading");
                dialog.setMessage("Please wait...");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(false);
                dialog.show();
                return dialog;
            default:
                return null;
        }
    }


 class DownloadFileAsync extends AsyncTask<String, String, String> {
     int count;
     URL myFileUrl;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected String doInBackground(String... aurl) {


            try {
                myFileUrl = new URL(imageUrl);
                HttpURLConnection conn = (HttpURLConnection) myFileUrl
                        .openConnection();
                int lenghtOfFile = conn.getContentLength();
                //conn.setDoInput(true);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                conn.connect();
                InputStream is = conn.getInputStream();
                bmImg = BitmapFactory.decodeStream(is);
                bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageUrl)
                        .getContent());
                bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);

                  byte data[] = new byte[1024];
                  System.out.println("mmmmmmmmmmmm");

                    long total = 0;
                    System.out.println("nnnnnnnnnn");
                    while ((count = ((InputStream) new URL(imageUrl)
                    .getContent()).read(data)) != -1) {
                        total += count;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        for(int l=0;l<4;l++){
                        if(listObject.get(l).getImage()!="")
                       image.setImageBitmap(bitmap);
                    }}

            }
            catch(Exception e){
                System.out.println(e);}

        return null;
        }

        protected void onProgressUpdate(String... progress) {
            dialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String unused) {
            image.setImageBitmap(bitmap);
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

        }
        }

却给出下面的异常:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

我找不出错误所在,请大家帮忙改正。

  • 写回答

2条回答 默认 最新

  • MrSimp1e 博客专家认证 2013-04-08 12:51
    关注

    继承自AsyncTask,注意AsyncTask最后的最后一个类型为Bitmap,然后 protected Bitmap doInBackground(String... aurl)函数返回获得的图片,
    最后在

    @Override protected void onPostExecute(Bitmap image) {
                image.setImageBitmap(image);
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    
            }
    

    ,
    还有image.setImageBitmap()不能放到doInBackground()中,因为doInBackground()不是运行在主线程。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路