Android:写入文件不起作用

发布于 2024-11-08 21:28:50 字数 1201 浏览 0 评论 0原文

谁能告诉我为什么下面的代码不起作用?我正在尝试将字符串附加到 text/html 文件的内容中。我只是按照android开发文档这里中的代码进行操作

package com.example.GetContentThenAppend;

import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;

public class GetContentThenAppend extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            String FILENAME = Environment.getExternalStorageDirectory() + "/EngagiaDroid/videos.html";
            String string = "<div style='color: blue; border: thin solid red;'>YO!</div>";

            FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
            fos.write(string.getBytes());
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Can anyone tell me why my code below does not work? I'm trying to append a string to the contents of a text/html file. I just followed the code in the android dev docs here

package com.example.GetContentThenAppend;

import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;

public class GetContentThenAppend extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            String FILENAME = Environment.getExternalStorageDirectory() + "/EngagiaDroid/videos.html";
            String string = "<div style='color: blue; border: thin solid red;'>YO!</div>";

            FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
            fos.write(string.getBytes());
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

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

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

发布评论

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

评论(3

泛泛之交 2024-11-15 21:28:50

我猜你的权限问题,你是否在清单中添加了写入文件权限。您可以通过将其添加到清单文件来做到这一点

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

I guess your problem in permissions,, have you added write to files permission in your manifest. You can do this by adding this to the manifest file

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
佼人 2024-11-15 21:28:50

确保您已创建 EngagiaDroid 目录。我建议你
保存文件之前调用

new File(Environment.getExternalStorageDirectory() + "/EngagiaDroid").mkdirs(); 

Make sure that you have directory EngagiaDroid created. I suggest that you
call

new File(Environment.getExternalStorageDirectory() + "/EngagiaDroid").mkdirs(); 

before saving file.

懵少女 2024-11-15 21:28:50

确保您拥有写入该文件的正确权限。在您的清单文件中,包含此行

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

Ensure you have the correct permissions to write to the file. In your manifest file, include this line

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