0

我是安卓新手。我在使用 AsyncTask 时遇到了麻烦。

我正在尝试创建一个图片库。在我的图库中有小尺寸的图片,当您单击其中一张时,该图片的大尺寸如下所示ImageView。我使用了两个不同的AsyncTask类。其中一个是从主机获取小尺寸图片,另一个是当您单击图库中的一张图片时从主机获取大尺寸图片。

我可以加载画廊图像,但是当我单击其中一个画廊图像时,我无法在ImageView.
如果您帮助我或有任何建议,我将不胜感激...

我的代码是:

public class GalleryView extends Activity{

    String[] smallpicsURL ={        
        "http://egtxxxxx.com/jImages/antartica1.jpg",
        "http://egtxxxxx.com/jImages/antartica2.jpg",
        "http://egtxxxxx.com/jImages/antartica3.jpg",
        "http://egtxxxxx.com/jImages/antartica4.jpg",
        "http://egtxxxxx.com/jImages/antartica5.jpg",
        "http://egtxxxxx.com/jImages/antartica6.jpg",
        "http://egtxxxxx.com/jImages/antartica7.jpg",
        "http://egtxxxxx.com/jImages/antartica8.jpg",
        "http://egtxxxxx.com/jImages/antartica9.jpg",
        "http://egtxxxxx.com/jImages/antartica10.jpg"
        };

    String[] bigpicsURL ={      
            "http://egtxxxxx.com/jImages/b_antartica1.jpg",
            "http://egtxxxxx.com/jImages/b_antartica2.jpg",
            "http://egtxxxxx.com/jImages/b_antartica3.jpg",
            "http://egtxxxxx.com/jImages/b_antartica4.jpg",
            "http://egtxxxxx.com/jImages/b_antartica5.jpg",
            "http://egtxxxxx.com/jImages/b_antartica6.jpg",
            "http://egtxxxxx.com/jImages/b_antartica7.jpg",
            "http://egtxxxxx.com/jImages/b_antartica8.jpg",
            "http://egtxxxxx.com/jImages/b_antartica9.jpg",
            "http://egtxxxxx.com/jImages/b_antartica10.jpg"
            };

    Bitmap pics[];
    Bitmap bigPic;
    ImageView imageView;    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);        
        Gallery ga = (Gallery)findViewById(R.id.galleryKBU);

        pics=new Bitmap[smallpicsURL.length];

        new FetchSmallBitmap().execute();   

        ga.setAdapter(new ImageAdapter(this));

        try {
            Thread.sleep(700);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }  

        imageView = (ImageView)findViewById(R.id.ivKBU);  

        ga.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getBaseContext(),"Çağdaş Eğitim Yuvası", Toast.LENGTH_SHORT).show();
                new FetchBigBitmap().execute(arg2);
                //imageView.setImageBitmap(bigPic);             
            }       
        });

    } 

    private Bitmap downloadPictures(int i) {
        String urlStr=smallpicsURL[i];          
        try { 
            URL url = new URL(urlStr);
            final URLConnection conn = url.openConnection(); 
            conn.connect(); 
            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
            final Bitmap bm = BitmapFactory.decodeStream(bis); 
            bis.close(); 
            return bm;
        } catch (IOException e) { 
           Log.d("DEBUGTAG",". Resim indirmede hata..."); 
        }     
        return null;
    }

    private Bitmap downloadBigPictures(int i) {
        String urlStr=bigpicsURL[i];            
        try { 
            URL url = new URL(urlStr);
            final URLConnection conn = url.openConnection(); 
            conn.connect(); 
            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
            final Bitmap bm = BitmapFactory.decodeStream(bis); 
            bis.close(); 
            return bm;
        } catch (IOException e) { 
           Log.d("DEBUGTAG",". Resim indirmede hata..."); 
        }     
        return null;
    }
  private class FetchSmallBitmap extends AsyncTask<Void, Integer, Bitmap[]> {

        private ProgressDialog dialog = new ProgressDialog(GalleryView.this);

        protected void onPreExecute(){
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.setMessage("Resimler yükleniyor");
            dialog.show();
        }

        @Override
        protected Bitmap[] doInBackground (Void ...params) {
            for(int i=0;i<10;i++){
                pics[i]=downloadPictures(i);
                publishProgress(10);                                
            }       
            return pics;
        }
         protected void onProgressUpdate(Integer ...progress){

             dialog.incrementProgressBy(progress[0]);

        }

         protected void onPostExecute(Bitmap[] mbitmap) {
             dialog.dismiss();                
         }
       }

        public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int imageBackground;

        public ImageAdapter(Context c) {
            ctx = c;
            TypedArray ta = obtainStyledAttributes(R.styleable.gallery_KBU);
            imageBackground = ta.getResourceId(R.styleable.gallery_KBU_android_galleryItemBackground, 1);
            ta.recycle();
        }

        public int getCount() {

            return pics.length;
        }


        public Object getItem(int arg0) {

            return arg0;
        }

        public long getItemId(int arg0) {

            return arg0;
        }

        public View getView(int arg0, View arg1, ViewGroup arg2) {
            ImageView iv = new ImageView(ctx);
            iv.setImageBitmap(pics[arg0]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new Gallery.LayoutParams(150,120));
            iv.setBackgroundResource(imageBackground);
            return iv;
        }

    }
      private class FetchBigBitmap extends AsyncTask<Integer, Integer, Bitmap> {

        private ProgressDialog dialog = new ProgressDialog(GalleryView.this);

        protected void onPreExecute(){
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.setMessage("Resimler yükleniyor");
            dialog.show();
        }

        @Override
        protected Bitmap doInBackground (Integer ...params) {                   
            imageView.setImageBitmap(downloadBigPictures(params[0]));
            return null;
        }
         protected void onProgressUpdate(Integer ...progress){

             dialog.incrementProgressBy(progress[0]);            

         }

         protected void onPostExecute(Bitmap x) {
             dialog.dismiss();

         }

    }
}

我收到这样的错误消息:

08-18 15:23:11.477: E/AndroidRuntime(15005): FATAL EXCEPTION: AsyncTask #2
08-18 15:23:11.477: E/AndroidRuntime(15005): java.lang.RuntimeException: An error occured while executing doInBackground()
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.lang.Thread.run(Thread.java:1096)
08-18 15:23:11.477: E/AndroidRuntime(15005): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.view.ViewRoot.checkThread(ViewRoot.java:2812)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.view.View.requestLayout(View.java:8125)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.view.View.requestLayout(View.java:8125)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.view.View.requestLayout(View.java:8125)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.view.View.requestLayout(View.java:8125)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.view.View.requestLayout(View.java:8125)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.widget.ImageView.setImageDrawable(ImageView.java:310)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.widget.ImageView.setImageBitmap(ImageView.java:324)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at com.kbu.bilgisistemi.GalleryView$FetchBigBitmap.doInBackground(GalleryView.java:258)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at com.kbu.bilgisistemi.GalleryView$FetchBigBitmap.doInBackground(GalleryView.java:1)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-18 15:23:11.477: E/AndroidRuntime(15005):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-18 15:23:11.477: E/AndroidRuntime(15005):    ... 4 more
08-18 15:23:11.687: E/WindowManager(15005): Activity com.kbu.bilgisistemi.GalleryView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a514158 that was originally added here
08-18 15:23:11.687: E/WindowManager(15005): android.view.WindowLeaked: Activity com.kbu.bilgisistemi.GalleryView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4a514158 that was originally added here
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-18 15:23:11.687: E/WindowManager(15005):     at android.app.Dialog.show(Dialog.java:241)
08-18 15:23:11.687: E/WindowManager(15005):     at com.kbu.bilgisistemi.GalleryView$FetchBigBitmap.onPreExecute(GalleryView.java:253)
08-18 15:23:11.687: E/WindowManager(15005):     at android.os.AsyncTask.execute(AsyncTask.java:391)
08-18 15:23:11.687: E/WindowManager(15005):     at com.kbu.bilgisistemi.GalleryView$1.onItemClick(GalleryView.java:102)
08-18 15:23:11.687: E/WindowManager(15005):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-18 15:23:11.687: E/WindowManager(15005):     at android.widget.Gallery.onSingleTapUp(Gallery.java:864)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:557)
08-18 15:23:11.687: E/WindowManager(15005):     at android.widget.Gallery.onTouchEvent(Gallery.java:839)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.View.dispatchTouchEvent(View.java:3766)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
08-18 15:23:11.687: E/WindowManager(15005):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1731)
08-18 15:23:11.687: E/WindowManager(15005):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1120)
08-18 15:23:11.687: E/WindowManager(15005):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
08-18 15:23:11.687: E/WindowManager(15005):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1715)
08-18 15:23:11.687: E/WindowManager(15005):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1787)
08-18 15:23:11.687: E/WindowManager(15005):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 15:23:11.687: E/WindowManager(15005):     at android.os.Looper.loop(Looper.java:123)
08-18 15:23:11.687: E/WindowManager(15005):     at android.app.ActivityThread.main(ActivityThread.java:4633)
08-18 15:23:11.687: E/WindowManager(15005):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 15:23:11.687: E/WindowManager(15005):     at java.lang.reflect.Method.invoke(Method.java:521)
08-18 15:23:11.687: E/WindowManager(15005):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-18 15:23:11.687: E/WindowManager(15005):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-18 15:23:11.687: E/WindowManager(15005):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

您不能在非 UI 线程上执行以下操作。您正在 doinbackground 中执行 Ui 操作。

    imageView.setImageBitmap(downloadBigPictures(params[0]));

将上一行移至 OnpostExecute。

您不需要 Asynctask 来设置图像

于 2012-08-18T12:24:01.750 回答