2014-10-08 75 views
0

我写了一个小应用程序,它在我的Android设备上创建了一个XML文件。不,我尝试将它从手机复制到我的Windows PC。在Windows资源管理器中,我无法看到此文件特定的文件,在我的手机上,我可以用各种文件浏览器查看此文件。当我重新启动手机时,该文件出现在Windows资源管理器中,但我无法将其复制到我的桌面。将文件从Android复制到Windows

这里是我的代码创建我的文件:

  String filename = "myfile.xml"; 
      String dir = Environment.getExternalStorageDirectory().getPath()+"/"+c.getResources().getString(R.string.app_name); 

      createDir(dir); 
      File file = new File(dir,filename); 

      FileWriter out=null; 
      try { 
       String xml = createXml(); 
       try { 

        out = new FileWriter(file); 
        out.write(xml); 
        out.close(); 
       } catch (Exception e) { 
        out.close(); 
        e.printStackTrace(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

我的猜测是,这个代码不释放文件句柄,所以机器人会MTP不能访问此文件。这也可以解释为什么在重新启动手机后显示文件并且可能会被删除(但无法传输到我的电脑)。

有什么建议出了什么问题?

回答

0

我想你应该刷新媒体扫描该文件

sendBroadcast( new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)) );

+1

不知道这样的事情存在。它不会在资源管理器中打开时刷新,但这已足够。谢谢。 – lueda 2014-10-08 09:06:10

相关问题