【问题标题】:Saving to File Android保存到文件 Android
【发布时间】:2014-04-06 19:26:17
【问题描述】:

我无法调试我的一些代码。它目前是一个简单的笔记商店应用程序,它试图在后台将字符串存储到文件中。但是当我在我的平板电脑上运行应用程序时,我得到确认代码运行的 toast,但我无法在平板电脑内部存储中找到该文件以证明它已存储?

我的代码:

public void onClick(View v) {
            //Gets the note detail from the fields.
            noteContent = content.getText().toString();
            noteTitle = title.getText().toString();
            Date date = new Date();

            if (noteContent.length() == 0 || noteTitle.length() == 0) {
                Toast.makeText(getApplicationContext(), "Please fill in both note title and content before saving",
                                Toast.LENGTH_LONG).show();
            } else {

            //Adds the notes together as an object of note.
            note createdNote = new note(noteContent, noteTitle);
            //Adds the note to the object array.
            noteArray.add(createdNote);
            //Testing
            note test = noteArray.get(0);
            String testTitle = test.getTitle();
            String testContent = test.getContent();
            System.out.println(testTitle);
            System.out.println(testContent);
            //Concatenating string.
            String noteString = testTitle + "-" + testContent;


            try {
                FileOutputStream FO = openFileOutput(fileName, MODE_WORLD_READABLE);
                OutputStreamWriter osw = new OutputStreamWriter(FO);
                try {
                    System.out.println(noteString);
                    osw.write(noteString);
                    osw.flush();
                    osw.close();
                    Toast.makeText(getBaseContext(), "NOTE_SAVED", Toast.LENGTH_LONG).show();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

我还包括读/写权限:s

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

【问题讨论】:

    标签: android file file-io


    【解决方案1】:

    使用 DDMS 的文件资源管理器并浏览到 /data/data/YOUR_PACKAGE/files/ Android: can not write to a txt file

    【讨论】:

      【解决方案2】:

      您不需要权限,因为您正在写入内部存储器。问题来了,为什么要这样打开文件:FileOutputStream FO = openFileOutput(fileName, MODE_WORLD_READABLE);

      【讨论】:

        猜你喜欢
        • 2015-04-30
        • 2014-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多