android 如何在JodConverter中阅读文档或pdf中的文本

s4n0splo  于 2022-12-25  发布在  Android
关注(0)|答案(1)|浏览(196)

Caused by:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.isDirectory()' on a null object reference

LocalOfficeManager officeManager = LocalOfficeManager.install();
try {
  // Start an office process and connect to the started instance (on port 2002).
  officeManager.start();
  File inputFile = new File("storage/emulated/0/COVID-19/111.pdf");
  wordtext = String.valueOf(JodConverter.convert(inputFile));
} catch (OfficeException e) {
  e.printStackTrace();
} finally {
  // Stop the office process
  OfficeUtils.stopQuietly(officeManager);
}

E/AndroidRuntime: FATAL EXCEPTION: main Process: handbook_multi_maker.TJ, PID: 17976 java.lang.RuntimeException: Unable to start activity ComponentInfo{handbook_multi_maker.TJ/TJ.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.isDirectory()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3107) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3250) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1947) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7032) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.isDirectory()' on a null object reference at org.jodconverter.local.office.LocalOfficeUtils.validateOfficeHome(LocalOfficeUtils.java:339) at org.jodconverter.local.office.LocalOfficeManager$Builder.build(LocalOfficeManager.java:169) at org.jodconverter.local.office.LocalOfficeManager.install(LocalOfficeManager.java:78) at TJ.SecondActivity.initWebView(SecondActivity.java:132) at TJ.SecondActivity.onCreate(SecondActivity.java:104) at android.app.Activity.performCreate(Activity.java:7327) at android.app.Activity.performCreate(Activity.java:7318) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3087) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3250) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1947) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7032) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

htrmnn0y

htrmnn0y1#

出现此错误是因为JODConverter的默认行为是自动检测您要使用的office(LibreOffice或Apache OpenOffice)安装。在您的特定情况下,没有找到office主目录(因此为空)。
因此,如果office安装位于自定义目录中,则可以使用officeHome属性。
下面是您的更新代码:

OfficeManager officeManager =
    LocalOfficeManager.builder()
        .officeHome("Path to your office home")
        .install()
        .build();
try {
  // Start an office process and connect to the started instance (on port 2002).
  officeManager.start();
  File inputFile = new File("storage/emulated/0/COVID-19/111.pdf");
  wordtext = String.valueOf(JodConverter.convert(inputFile));
} catch (OfficeException e) {
  e.printStackTrace();
} finally {
  // Stop the office process
  OfficeUtils.stopQuietly(officeManager);
}

相关问题