English 中文(简体)
缩略语 观点
原标题:Download pdf from googles online document Viewer

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);
        }
}
最佳回答

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.

问题回答

暂无回答




相关问题
How to upload and download file from a server using C#

I am developing a webpage in that the user can download their Resume for Edit. So I have a link to download the article. I use the following code for download. DataTable dt = user.getUserDetails(2); ...

Detecting PDF Download

I have recently added functionality for generating pdf reports to a web application. However the reports can be quite large and take some time to show the pdf download dialog. I want to show a spinner ...

Writing file to users giving sporadic error in IE

I have a very interesting issue with only specific IE implementations. I have an ASPX page that is used to write files down to the user, as part of the process the page uses the following code to ...

PHP - List of Excel files to download from Intranet Site?

I have a intranet site running PHP 5 that needs to list a folder containing only Excel files. Ideally the user needs to be able to input some search criteria (ie date) and the files would be filtered ...

Determine total size of SVN directory/trunk

Is there a way to count/calculate the total size of a svn directory if you were to checkout a revision? I have limited internet downloads so I need to know how big something is before I go and ...

Scala and html: download an image (*.jpg, etc) to Hard drive

I ve got a Scala program that downloads and parses html. I got the links to the image files form the html, Now I need to transfer those images to my hard drive. I m wondering what the best Scala ...

Downloading file with wxHTTP?

Im really stumped. Im using the wxHTTP class in wxWidgets to try and download two files. The first request succeeds but the second one fails when wxHTTP->GetInputStream is called. Between downloads, ...

热门标签