0

I would like to read a csv file. For that, I found CSVReader which I use like this :

List myEntries = null;
    try {
        AssetFileDescriptor descriptor = getAssets().openFd("file.csv");
        CSVReader csvReader = new CSVReader(new FileReader(descriptor.getFileDescriptor()), ',', '"');
        myEntries = csvReader.readAll();
        csvReader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

But I got this error: FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed.

My file is located in assets folder.

I have already seen this thread: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed It did not help me at all.

So, I must do something wrong, but what? Should I put my file on another folder and open it by another method?

Community
  • 1
  • 1
vital
  • 1,308
  • 3
  • 13
  • 28
  • Have you tried the suggestion mentioned in the referenced post (e.g. adding the file to assets folder in the apk manually)? Also, posting full stack trace of the exception would not hurt. – comodoro Jul 26 '12 at 21:31
  • You were right, it works great this way. By the way, since we are talking about compression... I have about one hundred of xml files (150MB) which are compressed because my apk file is only 4.5MB. Since they are compressed, I guess the application needs more time to access data in these files, am I right? And then, would it be faster to read if I put these files on the SD card? – vital Jul 27 '12 at 00:18
  • It depends on the way your application accesses the files. I can imagine there being some sort of caching, so it is not guaranteed to make a big difference. I guess the only way to get to know is to try it. – comodoro Jul 27 '12 at 08:14

1 Answers1

4

Android compresses all assets, except for the following types:

".jpg", ".jpeg", ".png", ".gif",
".wav", ".mp2", ".mp3", ".ogg", ".aac",
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
".amr", ".awb", ".wma", ".wmv"

If you have sufficient control over the build process, you can use the "-0" flag with zip or aapt to add the assets.

-0 specifies an additional extension for which such files will not be stored compressed in the .apk. An empty string means to not compress any files at all.

If you have the chance to do it with your CSVReader, deposit your csv file somewhere other than the assets folder, like sdcard.

Erol
  • 6,478
  • 5
  • 41
  • 55
  • Could you give me more detail? When I use the "-0" flag, I got the error command -0. The command is : ./aapt -0 db – EastBK Nov 29 '13 at 18:41