Download pdf from googles online document Viewer
I'm new to android and have been stuck on this problem for a long time...
I have an activity that loads a web view. In this web view it displays a pdf via googles online document viewer. This all works perfectly.
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 the link to the file is not .zip/.rar it is just a typical url ending in .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);
}
}
OK,
Instead of using AsyncTask to download the file and show its progress. I have compromised(for the moment), by using the ACTION_VIEW intent to download the file.
//download button
downloadButton = (Button)findViewById(R.id.download_button);
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent downloadIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf));
startActivity(downloadIntent);
}
});
All I need to do now is to find a way to store the files in different folders. But for now, this will have to do. Just said I'd offer this alternative incase someone else was having the same problem.
精彩评论