Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is acceptable file name in Android when storing to SD card

Tags:

android

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.

like image 537
Niko Avatar asked Jan 23 '26 11:01

Niko


2 Answers

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

like image 157
cgew85 Avatar answered Jan 25 '26 02:01

cgew85


I ended up replacing all illegal characters:

mFileName = mFileName.replaceAll("[|?*<\":>+\\[\\]/']", "_");
like image 45
Niko Avatar answered Jan 25 '26 00:01

Niko