[英]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.