2

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?

ClassA
  • 2,480
  • 1
  • 26
  • 57
  • `where should AsyncTask be called from?` Inside `onActivityResult` method inside `if (data.getData() != null)` block – Firoz Memon Jul 16 '17 at 10:15
  • @FirozMemon can you please explain to me how this will work? Will I be able to display the `progressDialog` over the gallery intent and how would I pass the selected video `Uri` to `AsyncTask`? – ClassA Jul 16 '17 at 10:26

2 Answers2

5

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!

Firoz Memon
  • 4,282
  • 3
  • 22
  • 32
  • I know this is not part of the question, but will you please update your code and show me how I can show the progress percentage while copying the file? – ClassA Jul 16 '17 at 11:06
  • Thank you for your reply, but I have 2 errors, `publishProgress(50);` - `Void cannot be applied to int` and at `super.onProgressUpdate(values);` at `values - Void cannot be applied to Integers`. I can also not use `@Overright` before onProgressUpdate – ClassA Jul 17 '17 at 06:04
  • I have asked a new question here --- https://stackoverflow.com/questions/45137748/show-progress-percentage-while-copying-file – ClassA Jul 17 '17 at 07:16
1

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();
    }
}
}
Mahdi Nouri
  • 1,391
  • 14
  • 29
  • I have tried copying large files, then the UI just freezes until the file is done copying, still without the progress showing. – ClassA Jul 16 '17 at 10:14
  • As I thought it would do, the UI freezes until the file is copied then the progress get displayed because you delayed the time. I want the progress to start showing when the user selects a file. – ClassA Jul 16 '17 at 10:21
  • @NewUser then you need to use AsyncTask where i said YOUR COPY PROCESS but without Handler – Mahdi Nouri Jul 16 '17 at 10:27
  • Ok can you please update your code showing me how I can get the selected video uri in AsyncTask? – ClassA Jul 16 '17 at 10:30