Android 中 CSV 文件的位置

发布于 2024-09-25 18:38:47 字数 125 浏览 3 评论 0原文

我在 Android 应用程序中读取 .CSV 文件时遇到问题。主要是“FileNotFound”错误。放置文件最容易的位置在哪里,以及它的地址是什么。我一直在几个不同的地方闲逛,但没有运气。

有人能指出我正确的方向吗?

I'm having trouble reading in from a .CSV file in an android app. Mainly a "FileNotFound" error. Where would be the easiest place to put the file, and what would be the address to it. I have been messing around with a few different places but have had no luck.

Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2024-10-02 18:38:47

您可能希望将其存储在可移动存储(SD 卡)上。首先,您需要检查存储是否可用,然后进行读/写。

该文档对此有一些注释: http:// /developer.android.com/intl/de/guide/topics/data/data-storage.html#filesExternal
(注意那里的 API 级别 8 的东西,例如 getExternalFilesDir,我还没有使用过它,但看起来很方便,如果你能保证 >=8。)

你可以检查存储是否在那里例如:

public boolean isExternalStorageAvail() {
      return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
   }

如果不存在,则显示一个Toast(至少)或以其他方式让用户知道。

然后读起来就像:

File f = new File(Environment.getExternalStorageDirectory() + "/mycsvlocation/myfile.csv");

然后你可以使用常规的File方法来检查它是否存在,是否可读等,然后使用它。

另请注意,文档建议您使用 SD 卡上的位置,例如“Android/data//files”,这是一个合理的建议,但许多应用程序只使用“/appname/”等。

还有一件事,当然,您只需安装 SD 卡并创建该结构并复制文件即可将文件放在那里。或者,您也可以通过编程方式(从您的应用程序)执行此操作,但听起来您已经拥有该文件,并且只想读取它。

You'll probably want it on the removable storage (SD card). First you'll want to check if the storage is available, then read/write.

The docs have some notes on this: http://developer.android.com/intl/de/guide/topics/data/data-storage.html#filesExternal
(Note the API level 8 stuff there, such as getExternalFilesDir, I haven't used that yet, but looks handy, if you can guarantee >=8.)

You can check if storage is there with something like:

public boolean isExternalStorageAvail() {
      return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
   }

If it's not there show a Toast (at a minimum) or otherwise let the user know.

Then read it like:

File f = new File(Environment.getExternalStorageDirectory() + "/mycsvlocation/myfile.csv");

Then you can use the regular File methods to check if it's there, it's readable, etc., and then use it.

Also note that the docs recommend you use a location on the SD card like "Android/data//files", and that's a reasonable recommendation, but many apps just use "/appname/" or such.

And -- one more thing, you can put the file there just by mounting the SD card and creating that structure and copying a file over, of course. Alternatively, you could do that programatically (from your app) as well, but it sounds like you already have the file, and just want to read it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文