I m New to android and have been stuck on this problem for long time...
我有一项带有网络观点的活动。 在这一网络中,它通过Agoogles在线文件浏览器展示了一个 pdf。 这一切都是完美的。
Below the Web View I have a download button(To download the pdf to the sd card). But the problem is, it is not downloading. Nothing seems to be happening. The progress Dialog appears for a few seconds, stays at 0% and then disappears.
Any help on how to get the file downloading would be very much appreciated. Thanks
PS与档案的联系不是.zip/。 很少,只是一种典型的ur,最后于......pdf。
public class ExamViewActivity extends Activity {
private static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button downloadButton;
private ProgressDialog mProgressDialog;
private String pdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.exam_paper_view);
//retrieve data(pdf link) from previous activity
Bundle b = getIntent().getExtras();
pdf = b.getString("pdf");
//webview
WebView webview = (WebView) findViewById(R.id.google_webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
//download button
downloadButton = (Button)findViewById(R.id.download_button);
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startDownload();
}
});
}
//start download process
private void startDownload() {
String url = pdf;
Log.d("MyLog","url = " + pdf);
new DownloadFileAsync().execute(url);
}
//create progress bar dialog
@Override
//On Create Dialog
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
//On Pre Execute
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
//Do In Background
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/"+pdf);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
//On progress update
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
//On post execute
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}