Reputation: 29
Hello i found this method that creates a file and add strings in it
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();
}
}
My question is how to call this method ? i tried something like this
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);
}
I don't understand the Context context part
Upvotes: 0
Views: 1687
Reputation: 24
If your using Fragment then do like this
generateNoteOnSD(getContext(),FILENAME,string);
if it is Activity do like this
generateNoteOnSD(YourActivityName.this,FILENAME,string);
Upvotes: 0
Reputation: 971
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);
}
Upvotes: 0
Reputation: 5349
use
generateNoteOnSD(YoureActivity.this,FILENAME,string);
Upvotes: 0
Reputation: 48307
you need to call the getApplicationContext() if you are in an Activity
generateNoteOnSD(getApplicationContext(),FILENAME,string);
Upvotes: 2