简体   繁体   中英

Show Progress Dialog while Processing OnActivityResult

I have written below codes where when user click attach button to select photos.

Below is code for same.

  Intent intent = new Intent();

                    intent.setType("*/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    startActivityForResult(Intent.createChooser(intent, "Select file to upload "), 1);

Below is code for OnActivityResult

 public void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == 1) {



            if (null != data) { // checking empty selection
                if (null != data.getClipData()) { // checking multiple selection or not
                    for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                        Uri uri = data.getClipData().getItemAt(i).getUri();
                        Log.i(TAG, "Path" + getPath(uri));
                        filespath.add(getPath(uri));

                        BitmapFactory.Options options = new BitmapFactory.Options();
                        Bitmap bitmap = BitmapFactory.decodeFile(getPath(uri), options);
                        bitmaps.add(bitmap);
                    }
                } else {
                    Uri uri = data.getData();
                }
            }
        }


    }

Now user can select multiple photos and I realized that when photos are more than 10, I get warning too much work done on main thread. when user click on Done button after selecting photos, I have Recycler view where I am showing thumbnail of images selected by user before final upload.

Now issue is when user click Done and till it shows Thumbnail, how can I show ProgressDialog , handle freeze screen and avoid warning work done on main thread.

To keep the parsing and loading work off the main thread you can just wrap everything in an AsyncTask. Given the small amount of code shown, I don't know what functions do what in the above, so this might have to be adjusted a bit. Move all the parsing logic, etc to do in the background as such:

        AsyncTask<Void, Void, List<Bitmap>>() {
        @Override
        protected  void onPreExecute()
        {
            //show progress dialog
            //Are you trying to prevent the user from clicking on the UI while updating?
            //You can do something such as:
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        }

        @Override
        protected List<Bitmap> doInBackground(Void... voids) {
            //perform the logic, this will return a list of all the Bitmaps to onPostExecute
            //Do NOT perform any ui logic here.
        }

        @Override
        protected void onPostExecute(List<Bitmap> bitmaps) {
            //cancel progress dialog.
            //Update the UI with the response.
            //Clear the window lock:
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM