Attempt to write Serializable Object with SAF gives as result an empty file

╄→гoц情女王★ 提交于 2020-06-28 05:50:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!