ProgressDialog使用AsyncTask卡住了

时间:2019-03-14 07:14:21

标签: android android-asynctask progressdialog

我正在使用AsyncTask下载图像,一切正常。现在,我面临的唯一问题是进度对话框卡住并冻结。以下是我的代码,任何人都可以帮助我。我在AsyncTask的执行前方法中添加了startLoading,在执行后添加了sideloading。在执行后,我还要打开一个底页,以便与社交媒体共享下载的图像。对不起,英语不好

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

0 个答案:

没有答案