2013-07-30 35 views
1

我是iOS开发人员,但也被授权更新我们公司的Android应用程序(所以我没有Android经验)Android应用程序当前从raw加载PDF,然后显示它们另一个PDF阅读器应用程序也安装在Android上...但是我想反而从互联网上获得pdf。如何从android中的特定URL打开pdf

这是用于显示本地存储的pdf的代码。

  if (mExternalStorageAvailable==true && mExternalStorageWriteable==true) 
     { 
     // Create a path where we will place our private file on external 
      // storage. 
      Context context1 = getApplicationContext(); 
      File file = new File(context1.getExternalFilesDir(null).toString() + "/pdf.pdf"); 

      URL url = new URL("https://myurl/pdf.pdf"); 
      URLConnection urlConnection = url.openConnection(); 
      InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
      OutputStream os = new FileOutputStream(file); 
      try { 


       byte[] data = new byte[in.available()]; 
       in.read(data); 
       os.write(data); 




       Uri path = Uri.fromFile(file); 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(path, "application/pdf"); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

       startActivity(intent); 

      } catch (IOException e) { 
       // Unable to create file, likely because external storage is 
       // not currently mounted. 
       Log.w("ExternalStorage", "Error writing " + file, e); 


       Context context2 = getApplicationContext(); 
       CharSequence text1 = "PDF File NOT Saved"; 
       int duration1 = Toast.LENGTH_SHORT; 
        Toast toast = Toast.makeText(context2, text1, duration1); 
        toast.show(); 
      } finally { 
       in.close(); 
       os.close(); 
      } 

     } 

最终的PDF文件会从网站和网站会要求PDF可以下载之前发送给它的HTML POST请求。我想我可以找出HTML文章,但现在我怎么能从互联网下载PDF并显示它。我试图改变URI指向的位置,但没有奏效,或者我错误地构造它。

也请记住出于安全原因,我不想显示该使用谷歌浏览器和网页视图

回答

1

你只需要从远程服务器来读取。我想尝试这样的:

URL url = new URL("http://www.mydomain.com/slug"); 
URLConnection urlConnection = url.openConnection(); 
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
try { 
    readStream(in); // Process your pdf 
} finally { 
    in.close(); 
} 

您可能还需要签出AndroidHttpClient类直接发出HTTP请求(GET或POST在您的应用程序)。

+0

我编辑了我的原始文章,以反映我如何试图合并此,但它导致应用程序崩溃..我做错了什么? – Ben

+0

错误日志将有助于您进行调试。 :-) – francoisr

+0

我结束了能够使用这个问题上的答案略有修改http://stackoverflow.com/questions/9759205/download-a-pdf-file-and-save-it-to-sdcard-和当时读它从 - 有 – Ben

相关问题