图片下载时显示 ProgressDialog
Show ProgressDialog while image downloading
我的应用程序具有 下载功能 需要一些时间来下载图像,因为我正在尝试在图像下载时添加 ProgressDialog
。 ProgressDialog
一直没出现请帮帮我
这是我保存图像的代码:
private String saveImage(Bitmap image) {
ProgressDialog pd = new ProgressDialog(Pop.this);
pd.setMessage("Downloading ...");
pd.show();
String savedImagePath = null;
String imageFileName = "vapor"+System.currentTimeMillis()+ ".png";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/vaporwave");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(Pop.this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
pd.dismiss();
}
return savedImagePath;
}
你应该使用一些后台任务,比如 AsyncTask
,你不应该在你的 UI 线程上做复杂的任务。(否则你的 UI 会在任务完成之前运气,所以你的 progressDialog
从不显示)
你可以阅读 AsyncTask Here
例如在异步中:
1) 在 onPreExecute
上显示您的进度对话框
2) 并在 onPostExecute
中关闭它
3) 并在 doInBackground
中完成您的工作(保存图像)(return 在此方法中输入将传递给 onPostExecute
)
public class saveImage extends AsyncTask<Bitmap,Void,String>{
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(Pop.this);
pd.setMessage("Downloading ...");
pd.show();
}
@Override
protected String doInBackground(Bitmap... params) {
Bitmap image = params[0];
String savedImagePath = null;
String imageFileName = "vapor"+System.currentTimeMillis()+ ".png";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/vaporwave");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(Pop.this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
pd.dismiss();
}
return savedImagePath;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//s is your savedImagePath
pd.dismiss();
}
}
你可以这样调用这个任务:
new saveImage().execute(yourBitmap);
我的应用程序具有 下载功能 需要一些时间来下载图像,因为我正在尝试在图像下载时添加 ProgressDialog
。 ProgressDialog
一直没出现请帮帮我
这是我保存图像的代码:
private String saveImage(Bitmap image) {
ProgressDialog pd = new ProgressDialog(Pop.this);
pd.setMessage("Downloading ...");
pd.show();
String savedImagePath = null;
String imageFileName = "vapor"+System.currentTimeMillis()+ ".png";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/vaporwave");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(Pop.this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
pd.dismiss();
}
return savedImagePath;
}
你应该使用一些后台任务,比如 AsyncTask
,你不应该在你的 UI 线程上做复杂的任务。(否则你的 UI 会在任务完成之前运气,所以你的 progressDialog
从不显示)
你可以阅读 AsyncTask Here
例如在异步中:
1) 在 onPreExecute
2) 并在 onPostExecute
3) 并在 doInBackground
中完成您的工作(保存图像)(return 在此方法中输入将传递给 onPostExecute
)
public class saveImage extends AsyncTask<Bitmap,Void,String>{
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(Pop.this);
pd.setMessage("Downloading ...");
pd.show();
}
@Override
protected String doInBackground(Bitmap... params) {
Bitmap image = params[0];
String savedImagePath = null;
String imageFileName = "vapor"+System.currentTimeMillis()+ ".png";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/vaporwave");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(Pop.this, "IMAGE SAVED", Toast.LENGTH_LONG).show();
pd.dismiss();
}
return savedImagePath;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//s is your savedImagePath
pd.dismiss();
}
}
你可以这样调用这个任务:
new saveImage().execute(yourBitmap);