Reputation: 202
I have these 2 lines of code that get called over and over. At the moment every time I want to use it I type it in. Was wondering how to put it into a callable method/class that I could instead something like FileLocation();
Thanks as always and I hope I have asked in the right way?
//App Location Tools
File StorageDirectory = Environment.getExternalStorageDirectory();
String StorageDownloads = StorageDirectory.getAbsolutePath() + "/Download";
Then to call it I say
File Allcastfile = new File(StorageDownloads, "Each file i use");
Upvotes: 0
Views: 63
Reputation: 9870
I don´t understand exactly what You want, and I guess that´s the reason why nobody give You an answer until now. But if I try to guess what exactly You mean, I think You only don´t want to type this line of code every time You need it, right? So, for this small two lines, there is only a little bit of optimization. You can do it like this to get everytime the same folder path:
private String getFolderPath(){
File StorageDirectory = Environment.getExternalStorageDirectory();
String StorageDownloads = StorageDirectory.getAbsolutePath() + "/Download";
return StorageDownloads;
}
and then You can always call:
String path = getFolderPath();
If this is not Your intension, You should make Your question a little bit more clear.
the other way is to make a global String:
private String StorageDownloads="";
and after setContentView
in Your activity (for example):
File StorageDirectory = Environment.getExternalStorageDirectory();
StorageDownloads = StorageDirectory.getAbsolutePath() + "/Download";
then You can use storageDownloads every time You need.
Upvotes: 1