looking for some solutions? You are welcome.

SOLVED: Progress Dailog stuck using asynctask

khimji :

I am using Asynctask for downloading images, everything is working fine. right now only issue I am facing is progress dialog is stuck and freeze. following is my code, can any one help me with that. I have added startLoading in pre execute and hideloading in post execute method of asyctask. in post execute i am opening one bottomsheet also to share downloaded images with social media. sorry for bad english

public class ImageSaveTask extends AsyncTask<String, Void, Void> {
        private Context context;

        private ImageSaveTask(Context context) {
            this.context = context;
        }

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



        @Override
        protected Void doInBackground(String... params) {


            prodImageUri.clear();
            int ii = 0;
            for (int k=0;k<resellerProductLists.get(globalPosition).getProductImageDtoList().size();k++) {
                //Toast.makeText(context,resellerProductLists.get(0).getProductImageDtoList().get(k).getImageUrl(),Toast.LENGTH_SHORT).show();
                Log.e("Image",resellerProductLists.get(globalPosition).getProductImageDtoList().get(k).getImageUrl());
                ii++;
                String img = "IMG-";
                String mPath = Environment.getExternalStorageDirectory().toString() + "/Temp/" + img + ii + ".jpg";
                File Directory = new File(Environment.getExternalStorageDirectory().toString() + "/Temp");
                Directory.mkdirs();
                try {
                    File file = Glide.with(context)
                            .load(resellerProductLists.get(globalPosition).getProductImageDtoList().get(k).getImageUrl())
                            .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            .get();

                    File imageFile = new File(mPath);

                    InputStream in = null;
                    OutputStream out = null;

                    try {
                        in = new BufferedInputStream(new FileInputStream(file));
                        out = new BufferedOutputStream(new FileOutputStream(mPath));

                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        out.flush();
                    } finally {
                        if (in != null) {
                            try {
                                in.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }

                        if (out != null) {
                            try {
                                out.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }

                    prodImageUri.add(Uri.fromFile(new File(String.valueOf(imageFile))));
                } catch (InterruptedException | ExecutionException | IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void res) {
            super.onPostExecute(res);
            //prodImageUri1.addAll(prodImageUri);


            if(single_rdb.isChecked())
            {
                createImageBitmap();
            }
            else if(allimg_rdb.isChecked())
            {
                temps=new ArrayList<>();
                for(int i=0;i<prodImageUri.size();i++)
                {
                    if(i==8)
                    {
                        break;
                    }
                    temps.add(prodImageUri.get(i));
                    // System.out.println("SIZE "+temps.size()+"TEST1 "+temps.get(i));
                    createImageBitmap();
                }

            }

            if(btn_click.equals("W"))
            {

                initSHareBottom("com.whatsapp", "WhatsApp");
            }
            else if(btn_click.equals("F"))
            {
                initSHareBottom("com.facebook.katana", "Facebook");
            }
            else if(btn_click.equals("default"))
            {
                addPhotoBottomDialogFragment =
                        new BottomSheetSharingFragment1();
                addPhotoBottomDialogFragment.show(((FragmentActivity)context).getSupportFragmentManager(),
                        "add_photo_dialog_fragment");
            }
            hideLoading();
        }
    }



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent