3

I am getting following error when trying to save to external cache dir (SD card) :

java.io.FileNotFoundException:
/mnt/sdcard/Android/data/myapp/cache/files/filenamexxx.png?1385609534:
open failed: EINVAL (Invalid argument) file:filenamexxx.png?1385609534

Using following code:

File sdCard = ctx.getExternalCacheDir();
File dir = new File(sdCard.getAbsolutePath() + "/files/");
dir.mkdirs();
File file = new File(dir, mFileName);
fos = new FileOutputStream(file);
fos.write ....

But saving to device when no SD card found works fine:

fos = ctx.openFileOutput(mFileName, Context.MODE_PRIVATE);
fos.write ....

Does the ?1385609534 in the file name end mess it up, when trying to save into SD cache?

Thanks.

Niko
  • 8,093
  • 5
  • 49
  • 85

2 Answers2

5

Some filesystems dont allow certain characters. You can check out which filesystem cant use which character here: Link

In your case it is most likely FAT32, so:

Any byte except for values 0-31, 127 (DEL) and: " * / : < > ? \ | + , . ; = [] (lowcase a-z are stored as A-Z). With VFAT LFN any Unicode except NUL

cgew85
  • 86
  • 1
  • 7
1

I ended up replacing all illegal characters:

mFileName = mFileName.replaceAll("[|?*<\":>+\\[\\]/']", "_");
Niko
  • 8,093
  • 5
  • 49
  • 85