Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:27:55+00:00 2026-06-10T07:27:55+00:00

I am new to Android. I have a trouble about using AsyncTask. I am

  • 0

I am new to Android. I have a trouble about using AsyncTask.

I am trying to create a picture gallery. In my gallery there are small size pictures and when you click on one of them, big size of that picture is shown in the below ImageView. I used two different AsyncTask class. One of them is fetching the small size pictures from the host, another one is fetching the big size pictures from host when you click on one of the pictures in the gallery.

I can load gallery images but when I click on one of gallery image I can’t display big size images in ImageView.
If you help me or for any advice, i will appreciate…

My code is:

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();

         }

    }
}

I get an error message like this:

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:27:57+00:00Added an answer on June 10, 2026 at 7:27 am

    You cannot do below operation on Non-UI thread. You are doing a Ui operation in doinbackground.

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

    Move above line to OnpostExecute.

    You dont need an Asynctask just to set the image

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am creating a new android application.i am using the table layout. I have
I am new to android development. I'm trying to have my string.xml file add
Pretty new to Mono for Android (C#), and I'm seeming to have trouble during
hi to all i am new to android i have to send my different
I am a bit new with android but I have some java experience. So
Ok, im fairly new to android but i have managed to teach myself the
I'm new to android apps development and I have a basic sqlite question. What
I am new in Android. I have develop an app. And now want to
I am new to Android. I have tried an sample application for status bar
Hi I am new to android and I am having an issue I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.