写内部专用存储在Android中Android

2023-09-04 10:05:38 作者:霸气的男子总是很低调

我想挽救我的应用程序中的一些数据的简单文本文件在内部专用存储区(应用程序用户preferences)。我已经经历了很多的问题就在这里(计算器)读取,并试图用没有成功提出的解决方案。最简单的解决方案,现在看来,将是一个建议这里: 的 http://developer.android.com/guide/topics/data/data-storage.html#filesInternal 但我不能得到这个工作我的测试设备。我也曾尝试创建使用在java.io.File中可用适当的方法方法的文件。我也曾尝试创建于SD卡中的文件具有相同的结果,失败。我曾尝试在其他的答案中列出了许多解决方案,继code和建议准确,发现同样的结果的指令。我开始觉得我缺少code一些重要的位,或者设置标志的地方,我已经设置在清单文件的权限:

I am trying to save some data from my app in a simple text file on the internal private storage area (app user preferences). I have read through many questions on here (StackOverflow) and tried the solutions suggested with no success. The simplest solution, it seems, would be the one suggested here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal but I cannot get this to work on my test device. I have also tried to create the file using the methods available in the java.io.File with the appropriate methods. I have also tried to create the file on the SDCard with the same result, fail. I have tried many solutions listed in other answers, following the code and instructions suggested exactly and find the same result. I am beginning to feel that I am missing some important bit of code, or a setting flag somewhere, I have set the permission in the manifest file:

  <uses-permission 
        android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

需要明确的是,我试图写入设备的内部,专用存储。这是一个包含一个名称的小文件,电话号码,和几个类型的 INT 标志。什么都的方法我用,我要么找到该文件不创建(或更改,如果我把手动在SD卡中的文件),或者我得到一个NullPointerException异常,当我尝试引用该文件或文件位置:

To be clear, I am trying to write to the device's internal, private storage. It is a small file containing a name, phone number, and a couple of type int flags. What ever method I use, I either find that the file did not create (or change if I place the file manually on the SDCard), or I get a NullPointerException when I try to reference the file or file location:

private              File   fILE = new File("Mydata", main.FILENAME);

or

private              File   fILE  = getDir("Mydata", 0);

我运行了code上的HTC Hero,更新来自Sprint最新的服务版本。任何帮助将不胜AP preciated,在此先感谢!

I am running the code on a HTC Hero, updated with the latest service release from Sprint. Any help would be GREATLY appreciated, Thanks in advance!

史蒂夫

更新(11年2月2日):使用EVO(API 8)我仍然得到一个NullPointerException异常。在code产生的例外是低于,为何我的应用程序不能访问内部存储有什么想法?我有关于使用两个API级别(API 7和8)三个不同的物理器件这个问题。

Update (2/2/11): Using a EVO (API 8) I still get a NullPointerException. The code generating the exception is below, any thoughts on why my app can't access the internal storage? I have this problem on three different physical devices using two API levels (API 7 and 8).

    File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");

更新2:11年2月4日 - 我发现,我看不到任何情况下对物理设备(数据目录)的文件结构。任何一个对此有什么想法?该设备配置正确,可以从Eclipse或亚行应用程序运行。

UPDATE 2: 2/4/11 - I have found that I cannot see the file structure on the physical device (data directory) under any circumstance. Any one have any thoughts on this? The device is properly configured and can run app from eclipse or adb.

更新3:(11年2月9日) - 我想我可能已经找到了问题是什么在这里,但我不知道如何处理它。我已经想通了,就对物理设备的/ data /目录的权限是: drwxrwx - X 。我不知道为什么是这样,或许真的要与Sprint?我发现这一套这种方式上的HTC Hero,三星史诗(银河S),和HTC EVO都在冲刺。出现问题是,DDMS和我的应用程序不具有读/写访问的目录。我这里需要弄清楚两件事情,为什么会是这样,如何​​来过度这一问题在野外。同样,这里的任何帮助将是真棒!

UPDATE 3: (2/9/11) - I think I may have found what the problem is here, but I am not sure about how to deal with it. I have figured out that the permissions on the /data/ directory on the physical devices are: drwxrwx--x. I am not sure why it is this way, maybe something to with Sprint? I have found this set this way on an HTC Hero, Samsung Epic (Galaxy S), and HTC EVO all on Sprint. The issue appears to be that DDMS and my app do not have r/w access to the directory. I need to figure out 2 things here, why it is like this and how to over come this issue in the wild. Again, any help here would be AWESOME!!

更新4:我认为去年二月是一个总的金发碧眼的时刻,我(请参见更新3)。我还没有根深蒂固,因此没有获得测试设备(废话!)。凡他SGS和EVO 4G已经经过更新后,结果还是一样。我仍然工作这个问题,并会尝试回到这里有一个更新很快(希望不到一年下一次)。

UPDATE 4: I think last February was a total blonde moment for me (see UPDATE 3). The test devices that I have are not ROOTed and hence no access (DUH!). After all the updates that he SGS and the EVO 4G have gone through, the result is still the same. I am still working this problem and will try and get back here with an update soon (hopefully less than a year next time).

推荐答案

不是直接回答你的问题,但无论如何:我注意到,你不想来存储数据吨您的文件。难道是使用共享preferences而不是一个充分的选择吗?

Not a direct answer to your question, but nevertheless: I noticed that you don't want to store tons of data in your file. Would it be a sufficient alternative to use the Shared Preferences instead?

也许更有趣:确实,当你写的共享preferences发生问题甚至文件,而不是

And perhaps even more interesting: does the problem occur even when you write to the Shared Preferences file instead?

http://developer.android.com /guide/topics/data/data-storage.html#$p$pf

 
精彩推荐