从库中复制文件时显示ProgressDialog

Cla*_*ssA 1 android progressdialog android-gallery android-asynctask

目前

我打开与画廊Intent,用户可以接着从后将该文件复制到指定文件夹的画廊视频.一切正常.

我希望ProgressBar在用户选择视频(在图库中)之后显示一个权限,然后progresbar.dismiss在文件完成复制后显示.

这是类似的问题,但没有答案(没有任何效果):

这就是我称之为画廊的方式:

public void selectVideoFromGallery()
{

    Intent intent;
    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    {
        intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
    }
    else
    {
        intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
    }

    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra("return-data", true);
    startActivityForResult(intent,SELECT_VIDEO_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)

我在progressbar.show();这里试过(上图),但显然它会在选择视频之前开始显示.

这是我的OnActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK)
    {
        if(data.getData()!=null)
        {


            //copy file to new folder
            Uri selectedImageUri = data.getData();
            String sourcePath = getRealPathFromURI(selectedImageUri);

            File source = new File(sourcePath);

            String filename = sourcePath.substring(sourcePath.lastIndexOf("/")+1);

            File destination = new File(Environment.getExternalStorageDirectory(), "MyAppFolder/CopiedVids/"+filename);
            try
            {
                FileUtils.copyFile(source, destination);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }


            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(destination)));


            Toast.makeText(getApplicationContext(),"Stored at:  "+"---"+sourcePath+"----"+"with name:   "+filename, Toast.LENGTH_LONG).show();



        }
        else
        {
            Toast.makeText(getApplicationContext(), "Failed to select video" , Toast.LENGTH_LONG).show();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在progressbar.show();这里尝试过(上图),但对话框从未显示过.

我的猜测是这样做AsyncTask,但随后在哪里打电话AsyncTask

这是我的ProgressDialog:

ProgressDialog progress;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    progress = new ProgressDialog(this);
    progress.setMessage("Copying Video....");
    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progress.setIndeterminate(true);

}
Run Code Online (Sandbox Code Playgroud)

我的问题

我需要澄清如何实现这一目标,AsyncTask如果没有,我能否做到这一点,如果没有,我应该AsyncTask从哪里调用?

Fir*_*mon 5

应该从哪里调用AsyncTask?

内部onActivityResult方法内部if (data.getData() != null)

if(data.getData()!=null)
{
    new MyCopyTask().execute(data.getData());
}
Run Code Online (Sandbox Code Playgroud)

-

 private class MyCopyTask extends AsyncTask<Uri, Integer, File> {
    ProgressDialog progressDialog;
    @Override
    protected String doInBackground(Uri... params) {
        //copy file to new folder
        Uri selectedImageUri = params[0];
        String sourcePath = getRealPathFromURI(selectedImageUri);

        File source = new File(sourcePath);

        String filename = sourcePath.substring(sourcePath.lastIndexOf("/")+1);

        File destination = new File(Environment.getExternalStorageDirectory(), "MyAppFolder/CopiedVids/"+filename);

        // call onProgressUpdate method to update progress on UI
        publishProgress(50);    // update progress to 50%

        try
        {
            FileUtils.copyFile(source, destination);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return destination;
    }

    @Override
    protected void onPostExecute(File result) {
        if(result.exists()) {
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(result)));

            Toast.makeText(getApplicationContext(),"Stored at:  "+"---"+result.getParent()+"----"+"with name:   "+result.getName(), Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(getApplicationContext(),"File could not be copied", Toast.LENGTH_LONG).show();
        }

        // Hide ProgressDialog here
        progressDialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        // Show ProgressDialog here

        progressDialog = new ProgressDialog(YourActivity.this);
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... values){
        super.onProgressUpdate(values);
        progressDialog.setProgress(values[0]);
    }

}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!