Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display name of file on progress bar while copy and paste.

What I have tried:

<pre>I want to display name of file in progress bar in while copy all files 
my code is.. 
        public CopyTask() {


            dialog = new ProgressDialog(MainActivity.this);
            dialog.setTitle("Copying " + getName() );

            dialog.setMessage("Copying a file to the internal storage, this can take some time!" + getName()  );
            dialog.setIndeterminate(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setCancelable(true);

            dialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialog.cancel();
                    CopyTask.this.cancel(true);
                }
            });

        }
Posted
Comments
Suvendu Shekhar Giri 28-Nov-17 0:15am    
So, are you getting any error?
getName() is fetching you the correct file name?
Member 13544752 28-Nov-17 5:53am    
no. bcos i declare getName() method before progress bar.. But if i did not declare then it show error. What is the solution of it
David Crow 28-Nov-17 8:34am    
It does not matter where getName() is declared, before or after, Java will still be able to find it.

In your CopyTask() function, I do not see any code that is doing the actual copying.

When the dialog is displayed, do you see "Copying " in the title (and "Copying a file to the internal..." in the message)?
Member 13544752 28-Nov-17 22:38pm    
Here is Code for copy and paste..But my question is how can i show file name on progress bar which is copying..


public void CopyTask1(View v) {

foldername="Files1 " + System.currentTimeMillis();
if (adapter == null)
return;



int count = adapter.getCount();


for (int i = 0; i < count; i++) {
copyfiles(i);


}

}

private void copyfiles(int position) {
UsbFile entry = adapter.getItem(position);

Toast.makeText(MainActivity.this,"files"+adapter.getCount(),Toast.LENGTH_SHORT).show();



// return;
//}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)

!= PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE))
{


Toast.makeText(this, R.string.request_write_storage_perm, Toast.LENGTH_LONG).show();
} else

{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_EXT_STORAGE_WRITE_PERM);
}

return;
}

CopyTaskParam param = new CopyTaskParam();

param.from = entry;

File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ "Files1" +"/"+foldername );


f.mkdirs();

int index = entry.getName().lastIndexOf(".") > 0
? entry.getName().lastIndexOf(".")
: entry.getName().length();

String prefix = entry.getName().substring(0, index);
String ext = entry.getName().substring(index);
// prefix must be at least 3 characters
if(prefix.length() < 3)

{
prefix += "pad";
}

try {
param.to = File.createTempFile(prefix, ext, f);

}
catch (IOException e)
{
e.printStackTrace();
}
new CopyTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,param);


}
David Crow 29-Nov-17 16:35pm    
I would go about it thusly:

class CopyTask extends AsyncTask<Void, String, Void>
{
    ProgressDialog dialog = null;

    protected void onPreExecute()
    {
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setTitle("Copying file...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }

    protected Void doInBackground()
    {
        for (int x = 0; x < num_files_to_copy; x++)
        {
            String name_of_file_to_copy = array_of_files[x];
            publishProgress(name_of_file_to_copy);
            copyFile(...);
        }
    }

    protected void onProgressUpdate(String... values)
    {
        dialog.setMessage(values[0]);
    }

    protected void onPostExecute()
    {
        dialog.dismiss();
    }
}
I would also refrain from calling checkSelfPermission(), shouldShowRequestPermissionRationale(), and requestPermissions() each time through the loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900