从库中复制文件时显示 ProgressDialog
Show ProgressDialog when copying a file from gallery
目前
我用 Intent
打开图库,然后用户可以从图库中选择一个视频,然后将文件复制到指定的文件夹。这一切都很好。
我想在用户选择视频(在图库中)后立即显示 ProgressBar
,然后在文件复制完成后 progresbar.dismiss
。
这里有类似的问题,但没有答案(没有任何效果):
- How to display a dialog over gallery screen in android?
- Progress Bar Circle after user selects image from gallery in android?
我是这样称呼画廊的:
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);
}
我已经在此处(上方)尝试了 progressbar.show();
,但显然它会在选择视频之前开始显示。
这是我的 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();
}
}
}
我已经尝试 progressbar.show();
此处(上图),但对话框从未显示。
我的猜测是用 AsyncTask
来做这个,但是然后在哪里调用 AsyncTask
这是我的 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);
}
我的问题
我需要澄清如何实现这一点,我可以在没有 AsyncTask
的情况下做到这一点吗?如果不能,应该从哪里调用 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) {}
}
并像下面这样执行:
@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();
}
}
}
where should AsyncTask be called from?
onActivityResult
方法内部 if (data.getData() != null)
块
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]);
}
}
希望对您有所帮助!
目前
我用 Intent
打开图库,然后用户可以从图库中选择一个视频,然后将文件复制到指定的文件夹。这一切都很好。
我想在用户选择视频(在图库中)后立即显示 ProgressBar
,然后在文件复制完成后 progresbar.dismiss
。
这里有类似的问题,但没有答案(没有任何效果):
- How to display a dialog over gallery screen in android?
- Progress Bar Circle after user selects image from gallery in android?
我是这样称呼画廊的:
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);
}
我已经在此处(上方)尝试了 progressbar.show();
,但显然它会在选择视频之前开始显示。
这是我的 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();
}
}
}
我已经尝试 progressbar.show();
此处(上图),但对话框从未显示。
我的猜测是用 AsyncTask
来做这个,但是然后在哪里调用 AsyncTask
这是我的 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);
}
我的问题
我需要澄清如何实现这一点,我可以在没有 AsyncTask
的情况下做到这一点吗?如果不能,应该从哪里调用 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) {}
}
并像下面这样执行:
@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();
}
}
}
where should AsyncTask be called from?
onActivityResult
方法内部 if (data.getData() != null)
块
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]);
}
}
希望对您有所帮助!