How can I display the progress bar when copying files using Apache FileUtils? Android

I use the Apache library to copy files from one directory to another. The problem I am facing is that I cannot figure out a way to update the execution line when copying a file. I thought I needed to use a callback, but there is no way to pass the callback to the copyfile method.

FileUtils.copyFileToDirectory(fileSrc, fileDest);
+4
source share
1 answer

I solved my problem as shown below, let it be useful for you.

if (mAdapter != null && mAdapter.getItemCount() > 0) {
    final ArrayList<StoryModel> selectedList = mAdapter.getSelectedData();

    if (selectedList.size() > 0) {

        final ProgressDialog pd = new ProgressDialog(getActivity());
        pd.setMessage("Saving Stories....");
        pd.show();

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    for (StoryModel imageModel : selectedList) {
                        String srcFilePath = imageModel.getUrl();
                        Log.d(TAG, "onOptionsItemSelected: " + srcFilePath);
                        org.apache.commons.io.FileUtils.copyFileToDirectory(new File(srcFilePath), new File(FileUtils.getAppPath(mContext)));
                    }
                } catch (IOException ignored) {

                }
                Log.d(TAG, "run: Dismissed....");
                pd.dismiss();
            }
        });
        t.start();
    } else {
        Toast.makeText(mContext, R.string.string_error_select_story_to_download, Toast.LENGTH_LONG).show();
    }
} else {
    Toast.makeText(mContext, R.string.string_error_select_story_to_download, Toast.LENGTH_LONG).show();
}

Thank:)

0
source

Source: https://habr.com/ru/post/1544826/


All Articles