一个关于写U盘后没数据的问题

xiezonglin 2019-07-05 11:24:46
系统是我自己编译的,有修改写u盘权限
现在的问题就是,我写u盘文件后,直接拔出来,到电脑上,文件有在,但是没数据
如果我在存储那边卸载u盘,或者把安卓关机再拔出u盘,那么就有数据
但是很难要求客户这么做,有没有办法把数据写入u盘而不是缓存?
我的代码如下
public static void writeSDFile_UTF8(Context ctx,String fileName, String write_str) throws IOException{

File file = new File(fileName);
if (!file.exists()) file.createNewFile();//建立文件

FileOutputStream fos = new FileOutputStream(file);
if (file.length() < 1) {

final byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };

fos.write(bom);

}
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(write_str);
osw.flush();
osw.close();
fos.flush();
fos.close();
MediaScannerConnection.scanFile(ctx, new String[] { file.getAbsolutePath() }, null, null);
}
...全文
87 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_44135498 2019-07-20
  • 打赏
  • 举报
回复
您好,请问这个现象具体原因是什么呢?
xiezonglin 2019-07-05
  • 打赏
  • 举报
回复
已解决 加上fos.getFD().sync();即可
xiezonglin 2019-07-05
  • 打赏
  • 举报
回复
修改安卓源码来解决的方案也可以接受

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧