gpt4 book ai didi

android - 除非我 "Force Close"应用程序,否则无法在 Windows 中看到由 SD 卡上的 Android 应用程序编写的文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:10 24 4
gpt4 key购买 nike

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

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 上,我将看不到该文件!

现在,如果我转到设置应用程序管理应用程序“强制停止” ,我什至可以在 Windows 中看到该文件。

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

实际上文件正在正确写入(我认为),但由于 Android“后退”按钮的默认行为不会终止应用程序,而且看起来除非应用程序被终止,否则我无法在外部打开文件Android(在 Android 中,我仍然可以看到它,甚至可以打开它)。

那么,对于这种情况,最好的解决方案是什么?我应该在关闭时自动终止该应用程序吗?人们说严格不推荐使用 System.exit()。

最佳答案

您必须使用以下代码,以便您的存档将显示在 Windows 的文件系统或手机的图库中。

你有两种可能:

  1. 它更快,因为 Android 只更新传递的文件:

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

  2. 它更昂贵,因为 Android 将搜索传递的路径中的所有文件。您可以只传递文件夹的路径以减少文件的搜索:

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

关于android - 除非我 "Force Close"应用程序,否则无法在 Windows 中看到由 SD 卡上的 Android 应用程序编写的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7429087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com