Android,我应该将数据写入哪个设备的目录,以便能够将其读回?
我正在编写一个使用 Wi-Fi 的 Android 应用程序,因此我无法轻松调试到模拟器(没有 Wi-Fi 支持...;-),所以我使用我的真实设备(三星 Galaxy S)。 我希望能够读取我的应用程序写入的数据文件,以进行调试和测试。 如果我使用,比如说:
new File(getFilesDir(), "myfile.xml");
我将数据文件写入/data/data/MYPACKAGE/files/,但是该目录无法通过adb(也不能通过Eclipse的DDMS)访问。 我的设备未已取得 root 权限(如果可能的话,我希望避免取得 root 权限...;-) 我应该将数据文件写入哪里?
I'm writing an Android app which uses wi-fi, so I can't easily debug to emulator (no wi-fi support... ;-), so I go with my real device (a Samsung Galaxy S).
I would like to be able to read data files my app writes, to debug and test.
If I use, say:
new File(getFilesDir(), "myfile.xml");
I get my data file written to /data/data/MYPACKAGE/files/, but that directory is non accessible via adb (nor via Eclipse's DDMS).
My device is not rooted (and I'd prefer to avoid rooting it, if possible... ;-)
Where should I write my data file to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在开发过程中将文件放在 SD 卡上可能是有意义的,正式地你应该调用 getExternalStorageDirectory() 来查找它,当然需要外部存储权限。
或者,您可以在调试版本中授予对您的私有文件的公共访问权限;只是不要忘记在发货前将其关闭(据报道某些网络电话公司就是这样做的)。但是,这不会使私有文件可浏览,因为中间目录不可浏览,您只能通过 adb 的确切路径名来提取它们。
第三种选择是将数据保留为内部和私有,但具有调试功能以将其复制到 SD 卡进行分析。您甚至可以在单独的 .apk 中执行此操作,与第一个 .apk 建立共享用户 ID,这意味着您的应用程序不会发生任何更改。
It probably makes sense to put the files on the sdcard during development, formally you should call getExternalStorageDirectory() to find it and of course will need external storage permission.
Alternatively, you could give public access to your private files in the debug version; just don't forget to turn that off before you ship (as a certain Internet telephony company reportedly did). However, this will not make the private files browsable as the intervening directories are not, you would only be able to adb pull them via their exact path name.
A third choice would be to leave the data internal and private, but have a debug function to copy it over to the sdcard for analysis. You could even do this in a separate .apk establishing a shared user id with the first, meaning no changes at all to your application.
只需使用外部存储!
Simply use external storage!
您可以写入 SD 卡。您应该使用 getExternalStorageDirectory() 来获取您的SD卡的路径。您必须在清单中包含
才能执行此操作。You can write to your SDcard. You should use getExternalStorageDirectory() to get your SDcard's path. You will have to include the
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in your Manifest to do that.答案因您的 API 级别而异。查看文档中有关外部存储的部分以获得此问题的答案。
http://developer.android.com/guide/topics/data /data-storage.html#filesExternal
对于一个比较通用的答案,您应该存储文件的 sdcard 目录是从 getExternalStorageDirectory() 返回的目录(它应该是您的 sdcard 的根目录或可能是内部扩展存储的根目录)与我的 Captivate 一样),子目录为 /Android/data/your.package.name/files
哦,是的,正如另一张海报提到的,不要忘记清单中的 WRITE_EXTERNAL_STORAGE 权限。
The answer differs depending on your API level. Review the section in the documentation on external storage to get the answer for this.
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
For a somewhat generic answer, the sdcard directory that you should be storing files in is the directory returned from getExternalStorageDirectory() (which should be the root of your sdcard or possibly internal expanded storage as with my Captivate), with subdirectories of /Android/data/your.package.name/files
Oh yes, and as another poster mentioned, don't forget the WRITE_EXTERNAL_STORAGE permission in your manifest.