2014-10-07 75 views
0

我是一個新的android,我正在嘗試從服務器讀取pdf。我發現不同的方式,並嘗試了其中的大部分。我嘗試使用webview,使用谷歌文檔,但沒有適合我。我不喜歡使用另一個第三方或插件。在android中執行PDF閱讀器時出現錯誤

我發現這個代碼工作完美,但它從assets文件夾中讀取。

protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_books_view); 
CopyReadAssets(); 
} 

    private void CopyReadAssets() 
    { 

     AssetManager assetManager = getAssets(); 

     InputStream in = null; 
     OutputStream out = null; 
     File file = new File(getFilesDir(), "test.pdf"); 
     try 
     { 
      in = assetManager.open("test.pdf"); 
      out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 

      copyFile(in, out); 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch (Exception e) 
     { 
      Log.e("tag", e.getMessage()); 
     } 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(
       Uri.parse("file://" + getFilesDir() + "/test.pdf"), 
       "application/pdf"); 

     startActivity(intent); 
    } 


    private void copyFile(InputStream in, OutputStream out) throws IOException 
    { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) 
     { 
      out.write(buffer, 0, read); 
     } 
    } 

我試着將它修改爲:

public class TestActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 
     //Call an AsycTask so you don't lock the main UI thread 
     new RequestTask().execute(); 

    }//end onCreate 

    private class RequestTask extends AsyncTask<String, String, String> 
    { 
     //Background task 
     protected String doInBackground(String... uri) 
     { 
      //Stuff you do in background goes here 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response; 
      String responseString = null; 
      //------- 
      String fileName="test"; 
      String fileExtension=".pdf"; 
      try 
      { 
      URL url = new URL("my url"); 
      HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 
      String PATH = Environment.getExternalStorageDirectory() + "/mydownload/"; 
      File file = new File(PATH); 
      file.mkdirs(); 
      File outputFile = new File(file, fileName+fileExtension); 
      FileOutputStream fos = new FileOutputStream(outputFile); 
      InputStream is = c.getInputStream(); 
      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = is.read(buffer)) != -1) { 
       fos.write(buffer, 0, len1); 
      } 
      responseString = fos.toString(); 
      fos.flush(); 
      fos.close(); 
      is.close(); 
      } 
      catch (ClientProtocolException e) 
      { 
       //TODO Handle problems.. 
      } 
      catch (IOException e) 
      { 
       //TODO Handle problems.. 
      } 

      return responseString; 
     } 
     @Override 
     protected void onPostExecute(String result) 
     { 
      super.onPostExecute(result); 
      //Do anything with response.. 
      //Stuff you do after the asych task is done 

      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(
        Uri.parse(result), 
        "application/pdf"); 

      startActivity(intent); 
     } 
    } //end RequestTask class 

,但它給了我舉杯消息: ((不支持的文件類型))

有人可以幫我請,我花了幾乎整整一天的時間試圖找出問題。

回答

0

我更改使用整體更簡單的方法這個代碼

 //setContentView(R.layout.activity_main); 
     WebView webView=new WebView(GeneralHealthEducationArBooksViewActivity.this); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setPluginState(PluginState.ON); 

     //---you need this to prevent the webview from 
     // launching another browser when a url 
     // redirection occurs--- 
     webView.setWebViewClient(new Callback()); 

     String pdfURL = "your link"; 
     webView.loadUrl(
"http://docs.google.com/gview?embedded=true&url=" + pdfURL); 

     setContentView(webView); 

xml文件

<?xml version="1.0" encoding="utf-8"?> 
<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


</WebView> 

最後,pdf正在顯示:)

0

您必須使用HttpClient執行您的pdf文件的GET請求。這是一個例子返回一個字符串緩衝區,你必須重新創建你的PDF遠程文件讀取

class RequestTask extends AsyncTask<String, String, String>{ 

     @Override 
     protected String doInBackground(String... uri) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response; 
      String responseString = null; 
      try { 
       response = httpclient.execute(new HttpGet(uri[0])); 
       StatusLine statusLine = response.getStatusLine(); 
       if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
        response.getEntity().writeTo(out); 
        out.close(); 
        responseString = out.toString(); 
       } else{ 
        //Closes the connection. 
        response.getEntity().getContent().close(); 
        throw new IOException(statusLine.getReasonPhrase()); 
       } 
      } catch (ClientProtocolException e) { 
       //TODO Handle problems.. 
      } catch (IOException e) { 
       //TODO Handle problems.. 
      } 
      return responseString; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      //Do anything with response.. 
     } 
    } 

經過超過您可以撥打

new RequestTask().execute(url); 
+0

你能解釋一下嗎,我應該把這個類放到我的代碼中嗎,還是應該創建一個新的類?我很抱歉,如果我打擾你愚蠢的問題,但這是我第一次使用AsyncTask ..感謝 – 2014-10-08 09:21:13

+0

或者我應該把它放入CopyReadAssets函數?我是否還需要調用startactivity(intent)?我很困惑:( – 2014-10-08 10:39:16

+0

你必須創建一個新類並調用你想要檢索文檔的執行方法 – JoaoBiriba 2014-10-08 17:09:11