HungryBird
HungryBird

Reputation: 1147

Cannot find the path of assets in Android

I am a fresher in Android, I just tried to read an excel file, but have some troubles reading the Excel file in assets, it returns the log:

I/System.out: ************** I/System.out: file:/android_asset/data.xls ************** [File not found..!]

here is my code:

MainActivity.java

   public List<String> read(String key) throws IOException {
    List<String> resultSet = new ArrayList<String>();

    File inputWorkbook = new File("file:///android_asset/data.xls");
    System.out.println("**************");
    System.out.println(inputWorkbook);
    System.out.println("**************");
    if(inputWorkbook.exists()){
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over column and lines
            for (int j = 0; j < sheet.getRows(); j++) {
                Cell cell = sheet.getCell(0, j);
                if(cell.getContents().equalsIgnoreCase(key)){
                    for (int i = 0; i < sheet.getColumns(); i++) {
                        Cell cel = sheet.getCell(i, j);
                        resultSet.add(cel.getContents());
                    }
                }
                continue;
            }
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else
    {
        resultSet.add("File not found..!");
    }
    if(resultSet.size()==0){
        resultSet.add("Data not found..!");
    }
    return resultSet;
}

Here is the structure of my project: enter image description here

Could anyone help me figure out what's the problem?

Upvotes: 0

Views: 62

Answers (2)

PJain
PJain

Reputation: 566

You can add asset folder by Go to Files.

Step 2 : Go to Folders.

Step 3 : Create Assets Folder.

if you have added an asset folder properly than you put your Xls files in it. If it is done properly then you can get the file as an input stream from asset folder like this as below,

AssetManager assetManager = getAssets();
            //  open excel sheet
InputStream inputStream = assetManager.open("data.xls");

than this inputStream can help you ahead to solve your purpose

Upvotes: 1

Jacks
Jacks

Reputation: 828

You can add the assets inside the raw folder of your project. Below would be project structure once you change it.

enter image description here

This line of code can be used to get the handler towards it

 try {
      InputStream im =
                    getApplicationContext().getResources().openRawResource(R.raw.savings);
        } catch (Exception e) {
            Log.e("ERROR", e.getMessage());
        }

Upvotes: 0

Related Questions