中的文件应用程序

中的文件应用程序

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

问题描述

我通过我的 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 + "
");     \ Yeah, 'str' has a value there
writer.close();

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

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!

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

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

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

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")));

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

07-21 02:29