Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress Dialog not show on onActivityResult function

I have been developing application on that I got two activity.
first Activity Called Second to get Download url.

In OnActivityResult function of first Activity.there is a ProgressDialog that shows that status of image downloading using that url.

Problem is progress dialog not been shown in that activiy.

protected void onActivityResult(int requestCode, int resultCode, Intent imagePicked) 
    {       
        super.onActivityResult(requestCode, resultCode, imagePicked);
            if(resultCode==RESULT_OK)
            {

                ProgressDialog progressDialog=ProgressDialog.show(this, "", "Loading Image");
                switch(requestCode) 
                {
                    case CharacterSelector.GetFaceBookImage:
                        //Toast.makeText(this, "onFacebookimageresult", Toast.LENGTH_SHORT).show();
                        String ImageUrl=imagePicked.getStringExtra("DownloadLink");
                        WebService getImage=new WebService(ImageUrl);

                        Log.d("Selected Img url", ""+ImageUrl);  
                        InputStream inputStream;
                        try 
                        {
                            inputStream = getImage.getHttpStream(ImageUrl);
                            getImage=null;
                            Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
                            inputStream=null;
                             mybitmapDrawable=new BitmapDrawable(bitmap);
                            addImageUserImage(mybitmapDrawable.mutate());
                            progressDialog.dismiss();
                            bitmap=null;
                        }
                        catch (IOException e)
                        {
                            //Show server Error message:
                            e.printStackTrace();
                        }

                        break;

Regards, Kariyachan

like image 287
Dev.Sinto Avatar asked Apr 04 '11 09:04

Dev.Sinto


2 Answers

You should not execute long-running tasks (i.e. image fetching over network) on the main thread as this blocks UI redrawing.

Use AsyncTask to execute long-running tasks in the background and at the same time update the UI.

like image 183
Peter Knego Avatar answered Nov 14 '22 21:11

Peter Knego


If your WebService class downloads asynchronously, then the dismiss is called just after the show, so your progress dialog appears and disappears at the same time.

If your Webservice class download synchronously then it can cause ANR and your activity might be killed by the system. You have to use it inside another thread by using AsyncTask e.g.

like image 20
tbruyelle Avatar answered Nov 14 '22 21:11

tbruyelle