2011-07-06 38 views
1

我是新来的android和一直困在这个问题很长一段时间...从谷歌下载在线文档查看器

我有一个活动,加载一个Web视图。在这个网络视图中,它通过Google在线文档查看器显示pdf。这一切都完美。

下面的Web视图我有一个下载按钮(下载PDF文档的SD卡)。 但问题是,它不是下载。似乎没有任何事情发生。 进度对话框出现几秒钟,停留在0%然后消失。

任何有关如何获得文件下载的帮助将非常感激。 感谢

PS链接到文件不是.ZIP/.RAR它只是以.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); 
     } 
} 

回答

0

OK结束一个典型的网址,

而不是使用的AsyncTask下载文件并展示其进展。 我已经妥协(暂时),通过使用ACTION_VIEW意图要下载的文件。

//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); 
     } 
    }); 

所有我现在需要做的是找到一种方法将文件存储在不同的文件夹。但现在,这将不得不做。 只是说我会提供这种替代柜面别人有同样的问题。