复制文件时显示进度百分比 [英] Show progress percentage while copying file
问题描述
当前
我正在从图库中选择一个文件并将其复制到指定的文件夹中.复制时,我正在显示ProgressDialog
,而我正在使用AsyncTask
.
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
.
我正在尝试显示要复制的文件的进度,但是我遇到的问题是进度显示50%,并保持50%,直到文件完成复制.
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.
对此有很多问题,但所有问题都与从URL
下载有关.
There is a lot of questions about this, but all of them are related to downloading from URL
.
我的问题
如何获取正在复制的文件的当前进度并将其显示为百分比?
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();
}
}
}
推荐答案
手动复制并以百分比更新进度:
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();
这篇关于复制文件时显示进度百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!