简体   繁体   English

尝试在Android中打开Excel文件时不断收到FileNotFound异常

[英]Keep getting FileNotFound Exception when trying to open an excel file in Android

I have the following line of code in MainActivity.java: 我在MainActivity.java中有以下代码行:

Workbook myWorkBook = WorkbookFactory.create(new File("C://sampleWB.xlsx"));

which throws the following error: java.io.FileNotFoundException 它将引发以下错误:java.io.FileNotFoundException

I have also tried adding the excel file in the assets folder (app > src > main > assets), and tried the following code, but the same exception is being thrown. 我也尝试将excel文件添加到资产文件夹(应用程序> src>主>资产)中,并尝试了以下代码,但是抛出了相同的异常。

Workbook myWorkBook = WorkbookFactory.create(new File("sampleWB.xlsx"));

I have added the following in AndroidManifest.xml, but it did not resolve anything either: 我在AndroidManifest.xml中添加了以下内容,但它也无法解决任何问题:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

if it's of any help, this is my build.gradle file: 如果有帮助,这是我的build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.test.testProj"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation "org.apache.poi:poi:3.17"
    implementation "org.apache.poi:poi-ooxml:3.17"
    implementation 'com.fasterxml:aalto-xml:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

It seem that you are using "C://sampleWB.xlsx" as a file name. 似乎您使用的是"C://sampleWB.xlsx"作为文件名。 As described in this answer : and / are not valid characters for a file name. 如此答案中所述 :/不是文件名的有效字符。

To access files in your assets folder, refer to this answer : 要访问assets文件夹中的文件,请参考以下答案

Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it. 将您的文本文件放在Android项目下的/ assets目录中,并按以下方式使用AssetManager类进行访问。

AssetManager am = context.getAssets();
InputStream is = am.open("default_book.txt");

Or you can also put the file in the /res/raw directory, from where the file can be accessed by an id as follows 或者,您也可以将该文件放在/ res / raw目录中,可以通过该目录通过id进行访问,如下所示

InputStream is = 
context.getResources().openRawResource(R.raw.default_book);

First ,You need to request storage read permission to read file at runtime using 首先,您需要使用以下命令在运行时请求存储读取权限才能读取文件

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            READ_PERMISSION_REQUEST_CODE);

You can refer to this for more info - https://developer.android.com/training/permissions/requesting 您可以参考此以获取更多信息-https: //developer.android.com/training/permissions/requesting

Then if you want to access file from assets folder - 然后,如果您要访问资产文件夹中的文件-

AssetManager assetManager = getAssets();
assetManager.open("fileName");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试写入文件时出现FileNotFound异常 - FileNotFound exception when trying to write to a file 尝试创建文件时出现FileNotFound异常 - I'm getting FileNotFound exception while trying to create a file 尝试使用getServletContext()。getRealPath()读取文件时,FileNotFound异常 - FileNotFound Exception When Trying To Read File Using getServletContext().getRealPath() 尝试从资源获取文件时出现FileNotFound异常 - FileNotFound exception when trying to get File from resource FileNotFound读取excel文件时出现异常 - FileNotFound Exception while reading excel file 尝试读写时出现FileNotFound异常 - FileNotFound exception when trying to read and write 如何使我的Jbutton写入文件。 我不断抛出FileNotFound异常错误 - How do I make my Jbutton write to a file. I keep getting throws FileNotFound Exception Errors 尝试以编程方式在Eclipse中打开文件时获取类强制转换异常 - Getting class cast exception when trying to open a file in eclipse programatically 尝试使用Apache POI打开Excel文件时,为什么会出现异常“ IOException:ZIP条目大小太大”的问题? - Why am I getting exception “IOException: ZIP entry size is too large” when trying to open an Excel file using Apache POI? 带有将内置的plist文件的Android FileNotFound异常 - Android FileNotFound Exception with a plist file that will be built in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM