2017-02-22 14 views
0

Googleドキュメントを使用してwebviewでPDFリンクを表示しているときに、pdfのダウンロードと印刷オプションを非表示にしたいとします。それを達成するための考えがありますか?私はこれを試した。非表示Googleドキュメントを使用したWebviewでのPDFの印刷/ダウンロードオプション

mWebView.setWebViewClient(new Callback()); 

       String urlEncoded = null; 
       try { 
        urlEncoded = URLEncoder.encode(url, "UTF-8"); 
        url = "http://docs.google.com/viewer?url=" + urlEncoded; 

        Log.d("BrowserACtivity", "doc: "+url); 
        mWebView.loadUrl(url);     
       } catch (Exception e) { 
        Log.d("BrowserActivity", "Exc: "+e.toString());     
       } 

private class Callback extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      return(false); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 

      try { 
       mWebView.loadUrl("javascript:(function() { " + 
         "document.getElementsByClassName('ndfHFb-c4YZDc-GSQQnc-LgbsSe ndfHFb-c4YZDc-to915-LgbsSe VIpgJd-TzA9Ye-eEGnhe ndfHFb-c4YZDc-LgbsSe')[0].style.display='none'; })()"); 
      } catch (Exception e) { 
       Log.d("BrowserActivity", "onPageFinished -- Exc: " + e.toString()); 
      } 
     } 
    } 

しかし、私はそれを隠すことができませんでした。あなたは私に何か考えを提案できますか?前もって感謝します。

+0

文書のURLを共有することも可能です – jaffar

答えて

0

私のためにうまく動作するこのサンプルコードを試してください、ダウンロードボタンを無効にしてください。

public class PDFViewActivity extends AppCompatActivity { 

     WebView webView; 
     Bundle b; 
     String str_file_name = "",str_url = ""; 
     private ProgressDialog mProg; 

     Toolbar toolbar; 
     int MyDeviceAPI= Build.VERSION.SDK_INT; 
     Drawable upArrow; 


     @Override 
     public boolean onSupportNavigateUp(){ 
      finish(); 
      return true; 
     } 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_pdfview); 

      webView = (WebView) findViewById(R.id.wv_pdf_view); 

      toolbar = (Toolbar) findViewById(R.id.toolbar_pdf_view); 
      setSupportActionBar(toolbar); 

      getSupportActionBar().setTitle(getResources().getString(R.string.pdf_view)); 

      if(MyDeviceAPI>=21) { 
       upArrow = getResources().getDrawable(R.mipmap.back_arrow,null); 
      } 
      else 
      { 
       upArrow = getResources().getDrawable(R.mipmap.back_arrow); 
      } 

      if(MyDeviceAPI>=23) 
      { 
       upArrow.setColorFilter(getResources().getColor(R.color.color_white,null), PorterDuff.Mode.SRC_ATOP); 
      } 
      else 
      { 
       upArrow.setColorFilter(getResources().getColor(R.color.color_white), PorterDuff.Mode.SRC_ATOP); 
      } 

      getSupportActionBar().setHomeAsUpIndicator(upArrow); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


      if(MyDeviceAPI>=23) 
      { 
       toolbar.setTitleTextColor(getResources().getColor(R.color.color_white,null)); 
      } 
      else 
      { 
       toolbar.setTitleTextColor(getResources().getColor(R.color.color_white)); 
      } 

      mProg = MyProgressDailog.showProgressDialog(PDFViewActivity.this, getResources().getString(R.string.please_wait)); 

      b = getIntent().getExtras(); 
      if(b != null) 
      { 
       str_file_name = b.getString("FileName"); 
       str_url = "http://docs.google.com/gview?embedded=true&url="+ Utils.pdf_path+str_file_name; 
       webView.setWebViewClient(new MyBrowser()); 
       webView.getSettings().setBuiltInZoomControls(false); 
       webView.getSettings().setJavaScriptEnabled(true); 
       webView.getSettings().setSupportZoom(true); 
       webView.loadUrl(str_url); 
      } 
     } 

     private class MyBrowser extends WebViewClient { 

      @Override 
      public void onUnhandledKeyEvent(WebView view, KeyEvent ke) { 

       Log.e("Unhandled Key Event",ke.toString()); 
       mProg.cancel(); 

      } 

      @Override 
      public void onPageFinished(WebView view, String url) { 

       mProg.cancel(); 
       super.onPageFinished(view, url); 
       webView.loadUrl("javascript:(function() { " + 
         "document.getElementsByClassName('ndfHFb-c4YZDc-GSQQnc-LgbsSe ndfHFb-c4YZDc-to915-LgbsSe VIpgJd-TzA9Ye-eEGnhe ndfHFb-c4YZDc-LgbsSe')[0].style.display='none'; })()"); 
      } 

     } 
    } 
0
// Enable Javascript 
    WebSettings webSettings = mWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 

    // Force links and redirects to open in the WebView instead of in a browser 
    mWebView.setWebViewClient(new WebViewClient()); 

    // startWebView("http://google.com"); 

    mWebView.setDownloadListener(new DownloadListener() { 

     public void onDownloadStart(String url, String userAgent, 
            String contentDisposition, String mimetype, 
            long contentLength) { 
      DownloadManager.Request request = new DownloadManager.Request(
        Uri.parse(url)); 
      String mimeType = null; 
      request.setMimeType(mimeType); 
      request.allowScanningByMediaScanner(); 
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition,mimeType)); 
      DownloadManager dm = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE); 
      dm.enqueue(request); 
      Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important! 
      intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE 
      intent.setType("*/*");//any application,any extension 
      Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded 
        Toast.LENGTH_LONG).show(); 

     } 
    }); 
関連する問題