2014-02-06 2 views
0

아마존 url을 호출하여 안드로이드 webview에 pdf 파일을 표시하려고합니다. 하지만 흰 화면 만 보여줍니다. 아무것도로드 할 필요가 없습니다.안드로이드 웹보기 PDF를 볼 수 없습니다 Amazon 서비스 URL에서

다른 URL을 사용할 때 아마존은 webview에서 pdf 파일을 보여줍니다. 이 http://docs.google.com/gview?embedded=true&url= "+ myURL이

나는 또한뿐만 아니라 쓰기 URL에서 시도 :. 어떤 사람이 어떤 제안이있는 경우 그리고 잘 작동 http://www.durgasoft.com/Android%20Interview%20Questions.pdf

나를 인도 해주십시오

나는이 시도했다.

webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setPluginState(PluginState.ON); 
String url = Common.getPdfFromAmazon("52f3761d290c4.pdf"); 
webView.loadUrl(url); 

Android Menifest.xml also give Internet Permission: 
**<uses-permission android:name="android.permission.INTERNET" />** 

i can also try this "http://docs.google.com/gview?embedded=true&url=" + url ; 

Here is my logcat image. :

은 여기 참조 내 코드입니다

감사합니다.

+0

에서

public static void showPdfFromSdCard(Context ctx) { File file = new File(Environment.getExternalStorageDirectory() + "/pdf/original.pdf"); PackageManager packageManager = ctx.getPackageManager(); Intent testIntent = new Intent(Intent.ACTION_VIEW); testIntent.setType("application/pdf"); List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { ctx.startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(ctx, "No Application Available to View PDF", Toast.LENGTH_SHORT).show(); } } 

4 >> 전화 deletePdfFromSdcard() 당신은 당신의 스택 트레이스를 게시 할 수 있습니까? –

+0

오류나 경고가 표시되지 않습니다. –

답변

1

해결책이 그것을 위해 찾을 이일 조사 그래서 아마존 웹 서비스에서 처음 다운로드 PDF 파일에 시도하고 여기에 SD 카드를 연 다음 PDF 파일 내 코드

참고로 저장 후 : - 이 솔루션은 Amazon Web Service의 웹보기에서 PDF보기 전용입니다. 다른 웹 서비스에서 이 코드를 시도 : -

WebView webview=(WebView)findviewbyid(R.id.Webview); 
    String MyURL= "this is your PDF URL"; 
    String url = "http://docs.google.com/gview?embedded=true&url=" + MyURL; 
    Log.i(TAG, "Opening PDF: " + url); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl(url); 

를 -------------------------------- -------------------------------------------------- ------------> 아마존 웹 서비스의 경우이 코드

1>> Download PDF from Amazon WebService 

public static void DownloadFile(String fileURL, File directory) { 
    try { 

     FileOutputStream f = new FileOutputStream(directory); 
     URL u = new URL(fileURL); 
     HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
     c.setRequestMethod("GET"); 
     c.connect(); 

     InputStream in = c.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     while ((len1 = in.read(buffer)) > 0) { 
      f.write(buffer, 0, len1); 
     } 
     f.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

2

>>에서 PDF보기 시도하십시오 SD를 카드

public static void showPdfFromSdCard(Context ctx) 
{ 
    File file = new File(Environment.getExternalStorageDirectory()+"/pdf/MyPdf.pdf"); 
    PackageManager packageManager = ctx.getPackageManager(); 
    Intent testIntent = new Intent(Intent.ACTION_VIEW); 
    testIntent.setType("application/pdf"); 
    List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    Uri uri = Uri.fromFile(file); 
    intent.setDataAndType(uri, "application/pdf"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    try { 
     ctx.startActivity(intent); 
    } 
    catch (ActivityNotFoundException e) { 
     Toast.makeText(ctx, 
      "No Application Available to View PDF", 
      Toast.LENGTH_SHORT).show(); 
    } 

다운로드 PDF showPdfFromSdCard 메소드를 호출 한 후 . 아마존 웹 서비스에서 PDF를 표시 당신은에서 삭제를 PDF에 대한 SD 카드에서 여기 코드를 PDF 파일을 삭제 쇼 PDF 후

SD 카드

public static void deletePdfFromSdcard(){ 
    File file = new File(Environment.getExternalStorageDirectory()+"/pdf/MyPdf.pdf"); 
    boolean pdfDelete = file.delete(); 


} 
2

응용 프로그램 태그 외부의 매니페스트 파일에 인터넷 권한을 추가해야합니다.

<uses-permission android:name="android.permission.INTERNET" /> 
+0

우리는 이미 그것을 주었다. 빠른 응답을 가져 주셔서 감사합니다. –

+0

만약 다른 아이디어가 있다면 저를 도와주세요. –

+0

오류 메시지가 없거나 스택 추적 오류로 인해 많은 도움을받을 수없는 경우 ... –

5

먼저 다운로드를 필요로 PDF를 저장 장치를 열고 PDF 판독기/뷰어 응용 프로그램을 통해 장치에서 사용할 수 있습니다.

1 >> 다운로드 프로세스를 호출하고 아마존 웹 서비스 URL을 전달하려면 DownloadFileAsync()으로 전화하십시오.

new DownloadFileAsync().execute(url); 

2 >> 다운로드 PDF 프로세스를 AsyncTask에서 수행하십시오.

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

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(final String... aurl) { 

     try { 
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
      File dir = new File(extStorageDirectory, "pdf"); 
      if(dir.exists()==false) { 
       dir.mkdirs(); 
      } 
      File directory = new File(dir, "original.pdf"); 
      try { 
       if(!directory.exists()) 
        directory.createNewFile(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 

      URL url = new URL(aurl[0]); 
      URLConnection conexion = url.openConnection(); 
      int lenghtOfFile = conexion.getContentLength(); 
      conexion.connect(); 
      conexion.setReadTimeout(10000); 
      conexion.setConnectTimeout(15000); // millis 

      FileOutputStream f = new FileOutputStream(directory); 

      InputStream in = conexion.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = in.read(buffer)) > 0) { 
       f.write(buffer, 0, len1); 
      } 
      f.flush(); 
      f.close(); 
      in.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 

    } 

    @Override 
    protected void onPostExecute(String unused) { 
    } 
} 

3 >> pdf 다운로드 후 showPdfFromSdCard()로 전화하십시오.당신의 onResume()

public static void deletePdfFromSdcard(){ 
    File file = new File(Environment.getExternalStorageDirectory()+"/pdf/original.pdf"); 
    boolean pdfDelete = file.delete(); 
} 
관련 문제