Reputation: 5986
I want to programmatically access a specific file which will be included in my project folder. Is there a way to do this? If so, where in my project folder do I put the file, and what is some simple code to get its file path?
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
java.io.File spreadsheet = new java.io.File("Untitled spreadsheet.xlsx");
String filePath = spreadsheet.getAbsolutePath();
System.out.println("file path is"+filePath);
URL fileURL = getClass().getClassLoader().getResource("Untitled spreadsheet.xlsx");
String filePath2 = fileURL.getPath();
System.out.println("file path2 is"+filePath2);
java.io.File fileContent = new java.io.File(filePath);
FileContent mediaContent = new FileContent("application/vnd.ms-excel", fileContent);
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("application/vnd.ms-excel");
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
if (file != null) {
showToast("File uploaded: " + file.getTitle());
}
else
;
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
Upvotes: 7
Views: 42580
Reputation: 1834
So you want to access a file internal to your app; and you want to do so directly, rather, that is, from an Android Context (and then with a [android.|<package_name>.]R.<resource_type>.<resource_name>
).
You have two choices as to location: the res/raw
folder or assets/
folder (outside of the res
parent).
To choose between the two note from https://developer.android.com/guide/topics/resources/providing-resources.html
Arbitrary files to save in their raw form. To open these resources with a raw
InputStream
, callResources.openRawResource()
with the resource ID, which isR.raw.filename
.However, if you need access to original file names and file hierarchy, you might consider saving some resources in the
assets/
directory (instead ofres/raw/
). Files inassets/
aren't given a resource ID, so you can read them only usingAssetManager
.
To access a file in res/raw/
directly rather, that is, from an Android Context (and then with a [android.|<package_name>.]R.<resource_type>.<resource_name>
) you can do something like this:
File file = new File("app/src/main/res/raw/country_data_from_world_bank.xml");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
Upvotes: 0
Reputation: 34397
Put the file in root folder of your project. Then get the File URL, Path and other details as:
File file = new File("test.txt");
String filePath = file.getAbsolutePath();
EDIT: Alternate way (if the file is in your classpath e.g. put the file in "src" folder, and make sure its moved in "bin" or "classes" folder after compilation):
URL fileURL = getClass().getClassLoader().getResource(fileName);
String fileName = fileURL.getFile();
String filePath = fileURL.getPath();
Upvotes: 5
Reputation: 83587
This depends a lot on what type of file you want to access. You can put the file in either assets
or an appropriate subdirectory of res
(see Difference between /res and /assets directories).
Upvotes: 0