ClassA
ClassA

Reputation: 2680

Show ProgressDialog when copying a file from gallery

Currently

I open the gallery with an Intent, the user can then pick a video from the gallery after which the file is copied to a specified folder. This all works fine.

I would like to display a ProgressBar right after the user selects a video (in the Gallery) and then progresbar.dismiss once the file is done copying.

Here is similar questions, but no answers (nothing that worked):

This is how I call the gallery:

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

I have tried progressbar.show(); here (above) but obviously it will start showing before the video is selected.

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

I have tried progressbar.show(); here (above) but the dialog never get displayed.

My guess is to do this with AsyncTask, but then where to call AsyncTask

and here is my 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);

}

My Question

I need clarification on how this can be achieved, will I be able to do this without AsyncTask and if not, where should AsyncTask be called from?

Upvotes: 2

Views: 1638

Answers (2)

Firoz Memon
Firoz Memon

Reputation: 4680

where should AsyncTask be called from?

Inside onActivityResult method inside if (data.getData() != null) block

if(data.getData()!=null)
{
    new MyCopyTask().execute(data.getData());
}

--

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

}

Hope it helps!

Upvotes: 5

Mahdi Nouri
Mahdi Nouri

Reputation: 1389

first of all create this asynctask class :

private class FileCopyProcess extends AsyncTask<Uri, Void, Boolean> {

    @Override
    protected String doInBackground(Uri... params) {
        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);
        try
        {
            FileUtils.copyFile(source, destination);
            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();
            return true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
             // PROCESS DONE
        }
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

and execute it like below :

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

         // SHOW PROGRESS

         new FileCopyProcess().execute(data.getData());

    }
    else
    {
        Toast.makeText(getApplicationContext(), "Failed to select video" , Toast.LENGTH_LONG).show();
    }
}
}

Upvotes: 1

Related Questions