我将图像保存到SD卡时如何制作加载屏幕

时间:2015-04-14 00:52:43

标签: android android-fragments android-gallery saving-data

我想知道在我将图像保存到手机时是否可以使用某种加载屏幕。目前,只要单击“保存”按钮,就需要一段时间来保存图像并且手机会冻结。这是正常的过程吗?

我看了一些启动画面教程,但我不认为这就是我要找的东西。

** 编辑 **

我是新手,所以我一直试图了解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()函数中更改了返回 true ,然后执行onPostExecute()函数。

1 个答案:

答案 0 :(得分:0)

听起来你在ui线程上写了你的数据。你想要做的是创建一个AsyncTask(例如:http://developer.android.com/reference/android/os/AsyncTask.html)。你要做的是打开一个进度对话框(http://developer.android.com/reference/android/app/ProgressDialog.html),然后在doInBackground方法中将数据写入sdcard,在doInBackground中你调用publishProgress,它将调用onProgressUpdate,这就是你所在的位置更新您的进度对话框以显示写入百分比已完成。