2013-07-30 3 views
1

저는 iOS 개발자이지만 회사의 안드로이드 애플 리케이션도 업데이트해야합니다 (안드로이드 경험이 거의 없기 때문에). 안드로이드 앱은 현재 PDF 파일을 raw에서로드 한 다음 또 다른 PDF 리더 응용 프로그램은 또한 안드로이드에 설치 ... 그러나 나는 대신 PDF 파일을 인터넷에서 얻고 싶습니다.안드로이드의 특정 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 게시 요청이 필요합니다. 나는 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(); 
} 

당신은 또한 (GET 또는 응용 프로그램에서 POST) 직접 HTTP 요청을 만들기 위해 AndroidHttpClient 클래스를 체크 아웃 할 수 있습니다 : 내가 좋아하는 뭔가를 시도 할 것입니다.

+0

나는 이것을 통합하려고 시도한 방법을 반영하기 위해 원래 게시물을 편집했지만 앱이 충돌하는 원인이되었습니다. 내가 잘못 했나요? – Ben

+0

오류 로그를 디버깅하는 데 도움이됩니다. :-) – francoisr

+0

나는이 문제에 대한 대답의 약간의 수정을 사용할 수있게되었다. http://stackoverflow.com/questions/9759205/download-a-pdf-file-and-save-it-to-sdcard- 그 다음에 거기에서 읽는 것 – Ben

관련 문제