国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android webview轉PDF的方法示例

2019-12-12 01:13:25
字體:
來源:轉載
供稿:網友

1.網上找了好多沒有顯示出來效果不錯,后來看到調用手機打印預覽,看了效果還不錯,就打算使用系統打印服務預覽下載

2.‘webView.createPrintDocumentAdapter()'得到打印的PrintDocumentAdapter有了該類就可以使用onWrite方法寫入制定的文件,但是這個方法需要傳入回調這個悲劇的是這個回調方法是hiden的我們沒辦法調用

3,字怎么解決呢,有連個方法

3.1 使用此開源庫替換自己的sdk 中的android.jar文件,就可以使用了

https://github.com/anggrayudi/android-hidden-api

3.2 使用dexmaker生成動態代理代理PrintDocumentAdapter.WriteResultCallback和PrintDocumentAdapter.LayoutResultCallback方法依賴地址

compile 'org.droidparts.dexmaker:dexmaker-mockito:1.5'

4.完整代碼如下:

File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/PDFTest.pdf");  File dexCacheFile;  // 獲取需要打印的webview適配器  PrintDocumentAdapter printAdapter;  PageRange[] ranges;  ParcelFileDescriptor descriptor;  /**   a* @param webView   */  private void printPDFFile(WebView webView) {    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {      /**       * android 5.0之后,出于對動態注入字節碼安全性德考慮,已經不允許隨意指定字節碼的保存路徑了,需要放在應用自己的包名文件夾下。       */      //新的創建DexMaker緩存目錄的方式,直接通過context獲取路徑      dexCacheFile = getDir("dex", 0);      if (!dexCacheFile.exists()) {        dexCacheFile.mkdir();      }      try {        //創建待寫入的PDF文件,pdfFilePath為自行指定的PDF文件路徑        if (file.exists()) {          file.delete();        }        file.createNewFile();        descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);        // 設置打印參數        PrintAttributes attributes = new PrintAttributes.Builder()            .setMediaSize(PrintAttributes.MediaSize.ISO_A4)            .setResolution(new PrintAttributes.Resolution("id", Context.PRINT_SERVICE, 300, 300))            .setColorMode(PrintAttributes.COLOR_MODE_COLOR)            .setMinMargins(PrintAttributes.Margins.NO_MARGINS)            .build();        //打印所有界面        ranges = new PageRange[]{PageRange.ALL_PAGES};        printAdapter = webView.createPrintDocumentAdapter();        // 開始打印        printAdapter.onStart();        printAdapter.onLayout(attributes, attributes, new CancellationSignal(), getLayoutResultCallback(new InvocationHandler() {          @Override          public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {            if (method.getName().equals("onLayoutFinished")) {              // 監聽到內部調用了onLayoutFinished()方法,即打印成功              onLayoutSuccess();            } else {              // 監聽到打印失敗或者取消了打印            }            return null;          }        }, dexCacheFile.getAbsoluteFile()), new Bundle());      } catch (IOException e) {        e.printStackTrace();      }    }  }  /**   * @throws IOException   */  private void onLayoutSuccess() throws IOException {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {      PrintDocumentAdapter.WriteResultCallback callback = getWriteResultCallback(new InvocationHandler() {        @Override        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {          if (method.getName().equals("onWriteFinished")) {            Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_SHORT).show();            // PDF文件寫入本地完成,導出成功            Log.e("onLayoutSuccess","onLayoutSuccess");          } else {            Toast.makeText(MainActivity.this,"導出失敗",Toast.LENGTH_SHORT).show();          }          return null;        }      }, dexCacheFile.getAbsoluteFile());      //寫入文件到本地      printAdapter.onWrite(ranges, descriptor, new CancellationSignal(), callback);    }else {      Toast.makeText(MainActivity.this,"不支持4.4.以下",Toast.LENGTH_SHORT).show();    }  }  @SuppressLint("NewApi")  public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler, File dexCacheDir) throws IOException {    return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class)        .dexCache(dexCacheDir)        .handler(invocationHandler)        .build();  }  @SuppressLint("NewApi")  public static PrintDocumentAdapter.WriteResultCallback getWriteResultCallback(InvocationHandler invocationHandler, File dexCacheDir) throws IOException {    return ProxyBuilder.forClass(PrintDocumentAdapter.WriteResultCallback.class)        .dexCache(dexCacheDir)        .handler(invocationHandler)        .build();  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通榆县| 天镇县| 桦川县| 长宁县| 峨眉山市| 南漳县| 天镇县| 宣武区| 南雄市| 盐津县| 襄垣县| 吴忠市| 沧州市| 凌云县| 绥中县| 屯留县| 湟源县| 阿城市| 湘阴县| 衡东县| 报价| 苍溪县| 磐石市| 孟津县| 普兰县| 淳安县| 凌源市| 科技| 北京市| 清涧县| 双流县| 上犹县| 南陵县| 涞源县| 高雄县| 黄浦区| 泗洪县| 溧水县| 朝阳市| 安泽县| 卓尼县|