问题
I want to use SAF to write Object files
To write a text files If I try:
DocumentFile txtfile=df.createFile("text", "myfile.txt");
Uri source = txtfile.getUri();
resolver = getContentResolver();
OutputStream os = resolver.openOutputStream(source);
PrintWriter out = new PrintWriter(new OutputStreamWriter(os));
out.print(somestring);
out.flush();
out.close();
the file is correctly created and written.
But if I try in a similar way to write a Serializable
object rather than a string with this code:
DocumentFile serialfile=df.createFile("unspecified", "myserializable");
Uri source = serialfile.getUri();
resolver = getContentResolver();
OutputStream os = resolver.openOutputStream(source);
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(os));
out.writeObject(someserializable);
out.flush();
out.close();
The file is created correctly, but it's totally empty regardless the fact the Serializable
object is valid for sure (If I try to write it with traditional FileOutputStream
methods, rather than SAF procedure above, it's written correctly)
来源:https://stackoverflow.com/questions/62362953/attempt-to-write-serializable-object-with-saf-gives-as-result-an-empty-file