这是一个关于stackoverflow的新问题。我想知道如何在android上使用Jodconverter库进行文件转换。我看到了很多例子和问题,但都是基于java的,没有回答android的具体问题,甚至连官方网站都没有。虽然我确实看到所有者自己在他发布的一个安卓应用中使用了这个库,因此可以使用JODconverter在安卓上进行转换。
以下代码来自android版本的官方网站:
OfficeManager officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager();
officeManager.start();
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
try{
converter.convert(new File("/sdcard/tsxt.doc"), new File("/sdcard/tsx465t.docx"));
}catch(Exception e){
e.printStackTrace();
}
officeManager.stop(); 运行时我在包含除unoil库之外的所有库时获得的crash的Logcat输出(因为添加unoil会导致我转换为Dalvik失败错误):
06-28 05:28:50.086: E/AndroidRuntime(28629): FATAL EXCEPTION: main
06-28 05:28:50.086: E/AndroidRuntime(28629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jopendocs/com.example.jopendocs.JODActivity}: java.lang.IllegalStateException: officeHome not set and could not be auto-detected
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.ActivityThread.access$700(ActivityThread.java:151)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.os.Looper.loop(Looper.java:137)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.ActivityThread.main(ActivityThread.java:5293)
06-28 05:28:50.086: E/AndroidRuntime(28629): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 05:28:50.086: E/AndroidRuntime(28629): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 05:28:50.086: E/AndroidRuntime(28629): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-28 05:28:50.086: E/AndroidRuntime(28629): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-28 05:28:50.086: E/AndroidRuntime(28629): at dalvik.system.NativeStart.main(Native Method)
06-28 05:28:50.086: E/AndroidRuntime(28629): Caused by: java.lang.IllegalStateException: officeHome not set and could not be auto-detected
06-28 05:28:50.086: E/AndroidRuntime(28629): at org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration.buildOfficeManager(DefaultOfficeManagerConfiguration.java:163)
06-28 05:28:50.086: E/AndroidRuntime(28629): at com.example.jopendocs.JODActivity.onCreate(JODActivity.java:22)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.Activity.performCreate(Activity.java:5250)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-28 05:28:50.086: E/AndroidRuntime(28629): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
06-28 05:28:50.086: E/AndroidRuntime(28629): ... 11 more发布于 2015-06-30 14:12:43
我认为这是不可能的,因为JODConverter需要有效的OpenOffice安装。在启动时,这个库尝试猜测OpenOffice安装文件夹(大多数时候,根据操作系统,OpenOffice位于特定的文件夹),但在Android下,我不确定这是否可能。我没有Android设备,我不知道OpenOffice在这个操作系统上是否可用。如果是,则必须设置officeHome属性(如果我是对的,请看org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration类)
发布于 2018-01-31 10:57:11
官网的代码和你的不一样:
final LocalOfficeManager officeManager = LocalOfficeManager.install();
try {
// Start an office process and connect to the started instance (on port 2002).
officeManager.start();
// Convert
JodConverter
.convert(inputFile)
.to(outputFile)
.execute();
} finally {
// Stop the office process
OfficeUtils.stopQuietly(officeManager);
} https://stackoverflow.com/questions/31094932
复制相似问题