我的应用将文件转换为字节。我想在进行转换时显示进度条。但是,仅在转换完成时才会显示进度条。这是代码:
public static void fileToByte(Context context) {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading..."); // Setting Message
progressDialog.setTitle("ProgressDialog"); // Setting Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
progressDialog.setCancelable(false);
progressDialog.show();
File file = new File(path);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
txt_out.setText(new String(bytes));
}
catch (FileNotFoundException e) {
Toast.makeText(context, "File Not Found" + e, Toast.LENGTH_LONG);
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
更新: 我使用Async并且它的工作在STYLE_SPINNER上没有旋转
public class FileToByte extends AsyncTask<Void , Void, Void> {
ProgressDialog progressDialog;
byte[] bytes;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading..."); // Setting Message
progressDialog.setTitle("ProgressDialog"); // Setting Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
progressDialog.setCancelable(false);
progressDialog.show();
}
protected Void doInBackground(Void... voids) {
File file = new File(path);
int size = (int) file.length();
bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
}
catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "File Not Found" + e, Toast.LENGTH_LONG);
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
txt_out.setText(new String(bytes));
progressDialog.dismiss();
}
}
答案 0 :(得分:0)
使用这个例子来做你需要的事情。
protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean> {
String email=null;
@Override
protected Boolean doInBackground(String... params) {
RestAPI api = new RestAPI();
boolean userAuth = false;
try {
// Call the User Authentication Method in API
JSONObject jsonObj = api.UserAuthentication(params[0],
params[1]);
//Parse the JSON Object to boolean
JSONParser parser = new JSONParser();
userAuth = parser.parseUserAuth(jsonObj);
email=params[0];
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("AsyncLogin", e.getMessage());
}
return userAuth;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = new ProgressDialog(getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Please Wait...");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setIndeterminate(true);
progressBar.show();
//Toast.makeText(context, "Please Wait...", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(Boolean result) {
//Check user validity
if (result) {
FragmentManager frag = getFragmentManager();
FragmentTransaction ftran = frag.beginTransaction();
Fragment fragprofil = new Profils();
ftran.replace(R.id.main_content, fragprofil);
ftran.commit();
progressBar.cancel();
}
else
{
Toast.makeText(context, "Not valid email/password ",Toast.LENGTH_SHORT).show();
progressBar.cancel();
}
}
}
致电使用:
new AsyncLogin().execute(email, password);