您好我找到了创建文件并在其中添加字符串的方法
public void generateNoteOnSD(Context context, String sFileName, String sBody) {
try {
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
我的问题是如何调用此方法?我试过这样的事情
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String FILENAME = "hello_file";
String string = "hello world!";
generateNoteOnSD(Context ,FILENAME,string);
}
我不理解Context上下文部分
答案 0 :(得分:2)
答案 1 :(得分:0)
使用
generateNoteOnSD(YoureActivity.this,FILENAME,string);
答案 2 :(得分:0)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String FILENAME = "hello_file";
String string = "hello world!";
generateNoteOnSD(YourActivityName.this ,FILENAME,string);
}
答案 3 :(得分:0)
如果您使用Fragment,那么
generateNoteOnSD(getContext(),FILENAME,string);
如果是活动就像这样
generateNoteOnSD(YourActivityName.this,FILENAME,string);