0

I want to save some Bytes with the FileOutputStream but it says the file name is too long (/mnt/sdcard/3m.png)..

I get the file name in UTF-8 coded Bytes, I create a String like:

String FilePath = new String(FilePathBytes, 0, FilePathBytes.length, "UTF-8");

This FilePath String throws the FileNotFoundException, if I set the FilePath directly

FilePath = "/mnt/sdcard/3m.png";

it saves my file..

How can I solve this?

Jan Knoblauch
  • 219
  • 1
  • 3
  • 20
  • Have a look at this http://stackoverflow.com/questions/5453708/android-how-to-use-environment-getexternalstoragedirectory – Nabin Aug 11 '14 at 10:53

2 Answers2

0

You can use following method to write to the file :

public static void writeToFile(File file, String string) {
    file.mkdirs(); // if your folder doesn't exist, this method (mkdirs) will create it.
    FileOutputStream out;
    PrintStream prt;
    try {
        out = new FileOutputStream(file);
        prt = new PrintStream(out);
        prt.println(string);
        prt.close();
    } catch (Exception e) {
        System.out.println("Write error");
    }
}

And how can you call this method ?

File file = new File("/mnt/sdcard/3m.png"); // or where is your file
String text = "Your message or bytes which will save to file";
writeToFile(file, text);
okarakose
  • 3,692
  • 5
  • 24
  • 44
  • I think the problem is not how I save the file (because it's working if I set the FilePath directly) - The problem is the decoding from bytes to string.. – Jan Knoblauch Aug 11 '14 at 10:56
0

I solved my problem - The Length of the string FilePath after the encoding from the bytes is: 65532 - That's the size of the FilePathBytes where I create the string from...

So it's to long (File name too long, FileNotFoundException), but the string is full of empty chars.. Now I try to remove them.

Jan Knoblauch
  • 219
  • 1
  • 3
  • 20