【发布时间】:2015-04-14 00:52:42
【问题描述】:
我想知道在我将图像保存到手机时是否可能出现某种加载屏幕。目前,一旦我单击“保存”按钮,需要一段时间才能保存图像并且手机会冻结。这是正常流程吗?
我查看了一些启动画面教程,但我认为这不是我想要的。
** 编辑 **
我是新手,所以我一直在尝试了解 AsynxTask 和 Progress Dialog 的工作原理。以下是我到目前为止所做的。每当保存图像的按钮时,都会调用此类。问题是我没有出现进度对话框,也不会保存。
public class UITask extends AsyncTask<Void,Void,Boolean> {
private ProgressDialog p;
private File destination;
private String filename;
private Bitmap encodedBitmap;
private Context context;
public UITask(File destination,String filename,Bitmap encodedBitmap,Context context){
this.destination = destination;
this.filename = filename;
this.encodedBitmap = encodedBitmap;
this.context = context;
this.p = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
p.setMessage("Saving image to SD Card");
p.setIndeterminate(false);
p.setProgressStyle(ProgressDialog.STYLE_SPINNER);
p.setCancelable(false);
p.show();
}
@Override
protected Boolean doInBackground(Void... voids) {
try {
FileOutputStream out = new FileOutputStream(destination);
encodedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
p.dismiss();
if(aBoolean)
{
Toast.makeText(context, "Download complete", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"Failed to save image",Toast.LENGTH_SHORT).show();
}
}
更正
我已经弄清楚为什么它不保存了。为此,我只是简单地更改了 doInBackground() 函数中的 return true ,然后执行 onPostExecute() 函数。
【问题讨论】:
-
设置图像,将数据保存在后台线程或 AsyncTask 中,完成后使用 onPostExecute() 或 runOnUiThread() 等机制将图像设置为其他内容。跨度>
标签: android android-fragments android-gallery saving-data