Android:写入文件不起作用
谁能告诉我为什么下面的代码不起作用?我正在尝试将字符串附加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你的权限问题,你是否在清单中添加了写入文件权限。您可以通过将其添加到清单文件来做到这一点
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
确保您已创建 EngagiaDroid 目录。我建议你
保存文件之前调用
。
Make sure that you have directory EngagiaDroid created. I suggest that you
call
before saving file.
确保您拥有写入该文件的
正确权限
。在您的清单文件
中,包含此行Ensure you have the
correct permissions
to write to the file. In yourmanifest file
, include this line