ClassA
ClassA

Reputation: 2680

Show progress percentage while copying file

Currently

I'm picking a file from the gallery and copying it to a specified folder. While copying I'm showing a ProgressDialog, I'm doing this with AsyncTask.

I'm trying to show the progress of the file being copied with percentage, but the problem I have is that the progress shows 50% and stay at 50% until the file is done copying.

There is a lot of questions about this, but all of them are related to downloading from URL.


My Question

How can I get the current progress of the file being copied and display it as percentage?


Please see what I have tried below:

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

    }else{

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

        }
    }
}

private class MyCopyTask extends AsyncTask<Uri, Integer, File> {
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.show();
    }


    @Override
    protected File 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);

        //onProgressUpdate(50);

        publishProgress(50);

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

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


    @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();
            progressDialog.dismiss();


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

    }



}

Upvotes: 1

Views: 3831

Answers (2)

beeb
beeb

Reputation: 1615

Copy manually and update the progress in percent:

InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(destination);

long lenghtOfFile = source.length();
byte[] buf = new byte[512];
int len;
long total;
while ((len = in.read(buf)) != -1) {
  total += len;

  publishProgress((int)((total*100)/lenghtOfFile));
  out.write(buf, 0, len);
}

in.close();
out.close();

Upvotes: 3

rgl
rgl

Reputation: 45

the wrong is in publishProgress(50); you have to publish progress for each percent like this publishProgress(i); i++;

Upvotes: -3

Related Questions