除非我“强制关闭",否则无法在Windows上看到由Android应用程序在SD卡上写入的文件.该应用程序 [英] Can't see a file in Windows written by an Android app on SD card unless I "Force Close" the app

查看:85
本文介绍了除非我“强制关闭",否则无法在Windows上看到由Android应用程序在SD卡上写入的文件.该应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样通过Android程序编写了一个文件:

I wrote a file through my Android program like this:

String file = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Files/hello.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(str + "\n");     \\ Yeah, 'str' has a value there
writer.close();

该程序完成其工作,并结束.现在,我点击了Android上的后退"按钮以关闭该应用程序.如果然后转到Android文件浏览器(例如Astro),则可以看到该文件,但是如果将SD卡安装在Windows上,则看不到该文件!

The program does its job and it finishes. Now I hit the back button on Android to close the application. If I then go to an Android file browser (like Astro) I can see the file, but if I mount the SD card on Windows, I can't see the file!

现在,如果我转到设置应用程序管理应用程序强制停止"< application> ,即使在Windows中我也可以看到文件.

Now, if I go to SettingsApplicationsManage Applications"Force Stop" <application>, I'm able to see the file even in Windows.

我应该怎么做才能使我在Windows中看到一个文件,如果该文件是由我的Android应用程序写在SD卡上的,并且我不想进入设置并每次都强制关闭.

What should I do so that I can see a file in Windows if the file is written by my Android app on the SD card and I don't want to go to settings and hit force close every time.

实际上文件是正确写入的(我认为),但是由于Android的后退"按钮的默认行为不会杀死该应用程序,除非该应用程序被杀死,否则我无法在外部打开文件Android(在Android中,我仍然可以看到它甚至打开它).

Actually the file is being written properly (I think), but since the default behaviour of Android 'back' button doesn't kill the app, and it looks like unless the app is killed I can't open the file outside Android (in Android, I'm still able to see it and even open it).

那么,这种情况下最好的解决方案是什么?关闭该应用程序后,我应该自动将其杀死吗?人们说严格不建议使用System.exit().

So, what would be the best solution for this case? Should I automatically kill the app when it is closing? People say that System.exit() is strictly not recommended.

推荐答案

有必要使用以下代码,以便您的存档将显示在Windows的文件系统或电话的图库中.

It´s necessary that you use the following code so that your archive will be shown in the file system of Windows or in the gallery of the phone.

您有两种可能性:

  1. 速度更快,因为Android仅更新传递的文件:

  1. It´s faster because Android updates only the file passed:

MediaScannerConnection.scanFile(this, new String[]{"your path here</"}, null, null);

价格昂贵,因为Android会搜索传递路径中的所有文件.您只能传递文件夹的路径以减少文件搜索:

It´s more expensive because Android will search all the files in the path passed. You can pass only the path of the folder to reduce the search of the files:

context.sendBroadcast(
    new Intent(Intent.ACTION_MEDIA_MOUNTED, 
               Uri.parse("file://" + "path of the folder")));

这篇关于除非我“强制关闭",否则无法在Windows上看到由Android应用程序在SD卡上写入的文件.该应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆