I would like to download files (documents) from Server (Parse). The download button is shown inside a listview, and invoking the downloadFile method as follows:
setOnClickListener:
holder.btn_dl.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
downloadFile(file_url_list[position], "aaa");
}
});
Method:
void downloadFile(String download_file_path, String file_name)
{
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MYFOLDER/";
try {
URL url = new URL(download_file_path);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(fullPath, file_name);
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
totalSize = urlConnection.getContentLength();
showProgressdialog(download_file_path);
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
pb.setMax(totalSize);
}
});
//create a buffer...
byte[] buffer = new byte[2048];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 )
{
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
// update the progressbar //
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
pb.setProgress(downloadedSize);
float per = ((float)downloadedSize/totalSize) * 100;
tv_download_message.setText("Downloaded " + downloadedSize + "KB / " + totalSize + "KB (" + (int)per + "%)" );
}
});
}
fileOutput.close();
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
// pb.dismiss(); // if you want close it..
}
});
} catch (final MalformedURLException e) {
showError("Error : MalformedURLException " + e);
e.printStackTrace();
} catch (final IOException e) {
showError("Error : IOException " + e);
e.printStackTrace();
}
catch (final Exception e) {
showError("Error : Please check your internet connection " + e);
}
}
void showError(final String err)
{
getActivity().runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(getActivity(), err, Toast.LENGTH_LONG).show();
}
});
}
void showProgressdialog(String file_path)
{
dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_file_download);
dialog.setTitle("Download Progress");
TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
tv_title.setText("Download file... ");
tv_download_message = (TextView) dialog.findViewById(R.id.tv_download_message);
tv_download_message.setText("Starting download...");
TextView tv_download_path = (TextView) dialog.findViewById(R.id.tv_download_path);
tv_download_path.setText(""+file_path);
tv_download_path.setMovementMethod(LinkMovementMethod.getInstance());
dialog.show();
pb = (ProgressBar)dialog.findViewById(R.id.progress_bar);
pb.setProgress(0);
pb.setProgressDrawable(getResources().getDrawable(R.drawable.progress_green));
}
Question:
I have tried the link for file_url_list[position]
in web broswer. The link works very well and the file can be downloaded successfully to my desltop.
However, the file cannot be downloaded from clicking the btn_dl
, and toast the below error message:
Error: Please check your internet connection.
android.os.NetworkOnMainThreadException
PS: Double checked that there is good Internet connection