简体   繁体   中英

Android printing not pdf files

I want to print some .txt files with android print manager

File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), String.valueOf(System.currentTimeMillis()) + ".txt");
    try {
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        writer.append("какой-то текст");
        writer.flush();
        writer.close();

        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
        printManager.print(file.getName(), new PrintFileAdapter(file), null);

    } catch (IOException e) {
        e.printStackTrace();
    }

this code throws exception

java.lang.RuntimeException: Cannot print a malformed PDF file
  at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:815)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Is it possible to print simple txt document?

欢迎您来尝试文本文件加载到一个WebView ,然后打印的内容WebView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM