gpt4 book ai didi

Android POI 读取 Microsoft Word 文档时出错 : Provider com. bea.xml.stream.EventFactory 未找到

转载 作者:行者123 更新时间:2023-12-02 02:18:16 24 4
gpt4 key购买 nike

我正在尝试在 Android 应用程序中使用 Apache POI,以便可以读取 MS Word 文件。我正在集成 POIA 库,该库允许在 Android 上使用 Apache POI ( https://github.com/SUPERCILEX/poi-android )。但是,当我运行代码并读取 Word 文件时,收到以下错误:

org.apache.poi.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found

下面是我的代码(我在第一行收到错误):

var document = XWPFDocument(uri?.let { contentResolver.openInputStream(it) });
var extractor = XWPFWordExtractor(document);
var documentText = extractor.text; // Retrieve the document's text

下面是我的 gradle(项目级别):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.60'
repositories {
google()
jcenter()

}
ext {
poiVersion = '3.17'
}
dependencies {
classpath 'com.android.tools:r8:1.4.93'
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

下面是我的 gradle(应用程序级别):

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.diffchecker"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
implementation 'com.google.android.gms:play-services-ads:18.3.0'
implementation "com.github.SUPERCILEX.poi-android:poi:$poiVersion"
}

下面是完整的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.diffchecker, PID: 2292
org.apache.poi.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found
at org.apache.poi.javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:72)
at org.apache.poi.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:178)
at org.apache.poi.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:92)
at org.apache.poi.javax.xml.stream.XMLEventFactory.newInstance(XMLEventFactory.java:30)
at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:161)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:97)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:116)
at com.example.diffchecker.MainActivity.onActivityResult(MainActivity.kt:249)
at android.app.Activity.dispatchActivityResult(Activity.java:7759)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4598)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4647)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
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:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
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)

最佳答案

我成功地运行了一个配置与您类似的应用程序。

  1. 首先,我从 poi-android 中排除了 stax,因为它可能已经过时(从而导致问题),Java 6 及更高版本实际上不再需要它。

应用程序/build.gradle

implementation ("com.github.SUPERCILEX.poi-android:poi:$poiVersion") {
exclude group: 'stax', module: 'stax-api'
}
  • 其次,在我的 Application 中类中,我更新了 3 个系统属性,如下所示:
  • MainApplication.kt

    override fun onCreate() {
    super.onCreate()
    System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl")
    System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl")
    System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl")
    ...
    }
  • 最后,我构建并运行了该应用 -> 它成功打开。
  • 关于Android POI 读取 Microsoft Word 文档时出错 : Provider com. bea.xml.stream.EventFactory 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996015/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com