4
I am trying to show a Progressdialog in the process of downloading a binary file, however this does not appear, and I get no error either. I will briefly explain the way I have structured code
In my Mainactivity I set a button to get that file... When we click on the button whose name is botao_getbscan, we see a Alertdialog, with a series of parameters for the user to define, in order to build a string that represents the URL to download the binary file. After setting these parameters the user presses the ok button of Alertdialog and it disappears, starting to download, as you can see below:
botao_getbscan = (Button)findViewById(R.id.button4);
botao_getbscan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Se houver internet avança...
if (isNetworkAvailable()) {
// Construção de um dialog para introduzir os valores de contrução do b-scan
LayoutInflater li = LayoutInflater.from(mContext);
View dialog_view = li.inflate(R.layout.getbscan_dialog,null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setView(dialog_view);
// Define o titulo e a mensagem do dialog
//(...)
// Define os botões do dialog
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Definição de uma série de parâmetros para construir uma string com o "GET" pretendido...
final String fullstring = "http://...................................;
// Inicia o download e armazena na memória interna...
boolean succed = StartDownload(fullstring);
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
dialog.cancel();
}
});
// Cria e mostra o alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
else {
AlertDialog.Builder saveDialog = new AlertDialog.Builder(MainActivity.this);
saveDialog.setTitle("No Internet Connection Detected");
saveDialog.setMessage("A connection to the internet is required to retrieve B-Scan. Please check your connection settings and try again.");
saveDialog.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
}
});
saveDialog.show();
}
}
});//End of Get B-Scan Button
The Startdownload method consists of the following:
public boolean StartDownload(String string){
BitmapDownloaderTask object = new BitmapDownloaderTask(MainActivity.this);
object.execute(string);
try {
byteArray = object.get();
return true;
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
return false;
}
} // End of StartDownload
Next I have my Asynctask in a different file which I called Bitmapdownloadertask.java, which basically defines my Bitmapdownloadertask class which extends an Asynctask. This is where I set up the Progress dialog, which I would like to appear after clicking on the OK button of my Alertdialog, since after that the Download process starts immediately.. However the Progress dialog does not appear and I also get no error. I have the following:
class BitmapDownloaderTask extends AsyncTask<String, Void, byte[]> {
private ProgressDialog dialog;
private Context context;
private byte[] byteArray;
//--------------------CONSTRUCTOR--------------------------------//
public BitmapDownloaderTask(Context context) {
this.context = context;
this.byteArray = null;
dialog = new ProgressDialog(context);
}
//--------------PRE EXECUTE--------------------------------------//
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Loading...");
dialog.show();
}
//--------------BACKGROUND---------------------------------------//
@Override
// Actual download method, run in the task thread
protected byte[] doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
return downloadBitmap(params[0]);
}
//--------------POST EXECUTE-------------------------------------//
@Override
// Once the image is downloaded
protected void onPostExecute(byte[] byteArray) {
super.onPostExecute(byteArray);
dialog.dismiss();
if(byteArray != null){
//pDialog.dismiss();
}else{
//pDialog.dismiss();
Toast.makeText(this.context,"B-scan doens't exist, or there was a connection problem!", Toast.LENGTH_SHORT).show();
}
}
static byte[] downloadBitmap(String url) {
// ......................
}
Thank you!
You implemented something in the method
downloadBitmap()
?– ramaral
@ramaral Sim implementei, is a simple download method from a url using Httpentity Httpresponse and Inputstream...
– MarcoAF