0

I have the following line of code in MainActivity.java:

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

which throws the following error: 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.

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

I have added the following in AndroidManifest.xml, but it did not resolve anything either:

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

if it's of any help, this is my build.gradle file:

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'
}
GomuGomuZoro
  • 313
  • 1
  • 4
  • 16

2 Answers2

2

It seem that you are using "C://sampleWB.xlsx" as a file name. 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:

Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it.

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

InputStream is = 
context.getResources().openRawResource(R.raw.default_book);
B. Plüster
  • 564
  • 3
  • 11
  • Thanks. For anyone else who might look at this, I only needed the following line to read the file correctly, after placing the excel file in the assets directory: InputStream is = getAssets().open("sampleWB.xlsx"); Then I created the workbook as follows: Workbook myWorkBook = WorkbookFactory.create(is); – GomuGomuZoro Jan 25 '19 at 20:00
1

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

Then if you want to access file from assets folder -

AssetManager assetManager = getAssets();
assetManager.open("fileName");
Vishal Arora
  • 2,524
  • 2
  • 11
  • 14