2016-09-13 50 views
0

検索や試しに何時間も費やしましたが、とにかく動作しません。 WebViewの内部にあるfragmentのPDFファイルを表示しようとしています。Android WebViewにPDFが表示されない

最初に、Googleローディングサークルに小さな行が表示されます。
Screenshot with loading line

すべてがロードされた後、WebViewには空白の画面しか表示されません。ここで

は、私のコードのfragment.javaです:

final View webViewLayout = layoutInflater.inflate(R.layout.webview_empty, null); 
    linearLayout.addView(webViewLayout); 
    WebView webview = (WebView) linearLayout.findViewById(R.id.webview); 
    String myPdfUrl = "gymnasium-wandlitz.de/vplan/vplan.pdf"; 
    //String url = "http://staglay.com/pdfjs/web/viewer.html"; 

    webview.getSettings().setJavaScriptEnabled(true); 
    String url = "http://docs.google.com/viewer?embedded=true&url="+myPdfUrl; 
    webview.setWebViewClient(new MyWebViewClient()); 
    webview.loadUrl(url); 

そして、私の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="fill_parent" 
android:layout_height="fill_parent"/> 

は私のLogcatです:

09-13 13:01:08.299 8501-8501/de.toeducate.tosub I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter> 
09-13 13:01:08.299 8501-8501/de.toeducate.tosub I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter> 
09-13 13:01:08.302 8501-8501/de.toeducate.tosub I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter> 
09-13 13:01:08.302 8501-8501/de.toeducate.tosub I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter> 
09-13 13:01:08.308 8501-8501/de.toeducate.tosub I/cr_LibraryLoader: Time to load native libraries: 1 ms (timestamps 5927-5928) 
09-13 13:01:08.308 8501-8501/de.toeducate.tosub I/cr_LibraryLoader: Expected native library version number "52.0.2743.98", actual native library version number "52.0.2743.98" 
09-13 13:01:08.319 8501-8501/de.toeducate.tosub V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) {a6e1879} 
09-13 13:01:08.319 8501-8501/de.toeducate.tosub I/cr_LibraryLoader: Expected native library version number "52.0.2743.98", actual native library version number "52.0.2743.98" 
09-13 13:01:08.319 8501-8501/de.toeducate.tosub I/chromium: [INFO:library_loader_hooks.cc(143)] Chromium logging enabled: level = 0, default verbosity = 0 
09-13 13:01:08.331 8501-8501/de.toeducate.tosub I/cr_BrowserStartup: Initializing chromium process, singleProcess=true 
09-13 13:01:08.389 8501-10510/de.toeducate.tosub W/cr_media: Requires BLUETOOTH permission 
09-13 13:01:08.427 8501-10516/de.toeducate.tosub D/ConnectivityManager.CallbackHandler: CM callback handler got msg 524290 
09-13 13:01:08.438 8501-8501/de.toeducate.tosub W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring 
09-13 13:01:08.453 8501-8501/de.toeducate.tosub I/cr_Ime: ImeThread is not enabled. 
09-13 13:01:08.477 8501-8501/de.toeducate.tosub W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring 
09-13 13:01:08.480 8501-8501/de.toeducate.tosub I/cr_Ime: ImeThread is not enabled. 
09-13 13:01:08.504 8501-10516/de.toeducate.tosub D/ConnectivityManager.CallbackHandler: CM callback handler got msg 524290 
09-13 13:01:08.527 8501-10523/de.toeducate.tosub E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY) 
09-13 13:01:08.547 8501-10523/de.toeducate.tosub W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc 
09-13 13:01:08.557 8501-10523/de.toeducate.tosub W/VideoCapabilities: Unrecognized profile/level 0/0 for video/mpeg2 
09-13 13:01:08.557 8501-10523/de.toeducate.tosub W/VideoCapabilities: Unrecognized profile/level 0/2 for video/mpeg2 
09-13 13:01:08.557 8501-10523/de.toeducate.tosub W/VideoCapabilities: Unrecognized profile/level 0/3 for video/mpeg2 
09-13 13:01:08.572 8501-10523/de.toeducate.tosub I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es 
09-13 13:01:08.619 8501-8501/de.toeducate.tosub W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 8501 
09-13 13:01:08.620 8501-8501/de.toeducate.tosub W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 8501 
09-13 13:01:08.904 8501-8501/de.toeducate.tosub W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 8501 
09-13 13:01:09.039 8501-8501/de.toeducate.tosub W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 8501 
+0

ショー 'webview_empty' xmlファイルe –

+0

@Logic投稿が更新されました –

+0

@PepeBellin私はちょうど私のコードを更新しました。問題を引き起こしたコード、そのXMLファイルではありませんでした。 – W4R10CK

答えて

1

更新:

onCreateView方法で

View view=inflater.inflate(R.layout.fragment_crime_list,container,false); 
     LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.linearLayout); 
     View webViewLayout = LayoutInflater.from(getActivity()).inflate(R.layout.webview, linearLayout,false); 



     WebView webView = (WebView) webViewLayout.findViewById(R.id.webview1); 
     linearLayout.addView(webView); 
     WebSettings webSettings = webView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     String myPdfUrl = "gymnasium-wandlitz.de/vplan/vplan.pdf"; 
     String url = "https://docs.google.com/viewer?embedded=true&url="+myPdfUrl; 
     webView.loadUrl(url); 
+0

これは正常に動作しています。 –

+0

フラグメントがあるので機能しません。 –

+0

@PepeBellin:更新された回答を確認してください –

0
WebView webview = (WebView) linearLayout.findViewById(R.id.webview); 
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "gymnasium-wandlitz.de/vplan/vplan.pdf"; 
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf); 

更新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"/> 

ここに多くのためのリンク:help

+0

は何も変更されていません –

+0

WebView webview =(WebView)webViewLayout.findViewById(R.id.webview); – W4R10CK

+0

はローディングサークルを表示し、最後に空白の画面を表示します –

-1
String myPdfUrl = "http://gymnasium-wandlitz.de/vplan/vplan.pdf"; 
webview.getSettings().setJavaScriptEnabled(true); 
String url = "http://docs.google.com/viewer?embedded=true&url="+myPdfUrl; 
webview.setWebViewClient(new MyWebViewClient()); 
webview.loadUrl(url); 
0

私はあなたに別の方法を提案する - Googleドキュメントを省略。

PdfViewerプロジェクトをご覧ください。 assets/pdfviewer/からプロジェクトにすべてのファイルをコピーします。

同じフォルダにあるファイルindex.htmlを追加します。

<!DOCTYPE html> 
<html> 
<head> 
    <link rel="stylesheet" href="file:///android_asset/pdfviewer/minimal.css"/> 
     <script type="text/javascript" src="file:///android_asset/pdfviewer/compatibility.js"></script> 
     <!-- horrible hack needs fix--> 

    <script type="text/javascript"> 
     var url = 'THE_FILE'; 
    </script> 

</head> 
<body> 

    <div> 
     <canvas id="the-canvas"/> 
    </div> 

<!-- Use latest PDF.js build from Github --> 
    <script type="text/javascript" src="file:///android_asset/pdfviewer/pdf.js"></script> 
    <script type="text/javascript" src="file:///android_asset/pdfviewer/customview.js"></script> 

</body> 
</html> 

は、お使いの携帯電話に.pdfファイルをダウンロードし、それをUriを取得します。

その後セットアップのWebView:

WebSettings settings = webView.getSettings(); 
settings.setJavaScriptEnabled(true); 
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) 
{ 
    settings.setAllowFileAccessFromFileURLs(true); 
    settings.setAllowUniversalAccessFromFileURLs(true); 
} 
settings.setBuiltInZoomControls(true); 
settings.setDisplayZoomControls(false); 
settings.setUseWideViewPort(true); 
settings.setLoadWithOverviewMode(true); 
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
webView.setWebViewClient(new WebViewClient()); 

そしてWebViewにファイルをロードします。

Uri path = Uri.parse(/**/); 
    InputStream ims = null; 
    FileOutputStream fileOutputStream = null; 
    try { 
     ims = getAssets().open("pdfviewer/" + INDEX_HTML); 
     String line = getStringFromInputStream(ims); 
     if (line.contains(FILE_INDICATOR)) { 
      line = line.replace(FILE_INDICATOR, path.toString()); 

      fileOutputStream = openFileOutput(INDEX_HTML, Context.MODE_PRIVATE); 
      fileOutputStream.write(line.getBytes()); 
      fileOutputStream.close(); 
     } 
    } catch (IOException e) { 
     try { 
      if (ims != null) { 
       ims.close(); 
      } 
      if (fileOutputStream != null) { 
       fileOutputStream.close(); 
      } 
     } catch (IOException inputOutputException) { 
      return; 
     } 
     return; 
    } 

    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      webView.loadUrl("file://" + getFilesDir() + "/" + INDEX_HTML); 
      webView.scrollTo(webView.getWidth()/2, 0); 
     } 
    }); 
関連する問題