https://developers.google.com/drive/api/v3/appdata#create_a_file_in_the_application_data_folder here is an example how to create a file using Google Drive API on the appData folder. I didn't find any example on how to create a txt file and manipulate it's content using the Google Drive API.
Any help is appreciated.
To create a txt filte in the appData folder
Modify the Name and filePath of sample code snippet in the documentation from json to txt and the mimeType from application/json to text/plain:
File fileMetadata = new File();
fileMetadata.setName("config.txt");
fileMetadata.setParents(Collections.singletonList("appDataFolder"));
java.io.File filePath = new java.io.File("files/config.txt");
FileContent mediaContent = new FileContent("text/plain", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
System.out.println("File ID: " + file.getId());
To manipulate the content of the file
See here for official information on how to upload the content.
Basically, for a single request you need to
obtain the resumable URI
perfom PUT request to it containing the file content in the request body
Specify the content length (mediaContent.setLength())
I do not have a Java snippet for this part, but I have done it before in Apps Script - I hope this is helpful to understand how you can manipulate the content of the file you crated in the appData fodler.
See also here for a detailed sample on how to upload a file with content in Java.
Related
I have an existing File object, created during my program. How can I use the drive API to upload this to a specific folder?
I understand it's something like this:
String folderId = "0BwwA4oUTeiV1TGRPeTVjaWRDY1E";
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
fileMetadata.setParents(Collections.singletonList(folderId));
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id, parents")
.execute();
System.out.println("File ID: " + file.getId());
But
How do I create the driveService object
How do I replicate the above
if I want to use a File object generated in my program, not one that
exists on my computer?
For Creating driveService(com.google.api.services.drive.Drive) class you need Google Single SignOn, And also you need to add the scope in Google console, and you need to use com.google.api.services.drive.Drive.Builder#Builder for creating the class.
For Auth process, you can refer this
For Complete Sample for Android, you can refer to this class
I want use google drive in my android app to backup folders that contain text, image and video files.
My problem is that while I can upload each text, image and video file separately using the Drive API, I can't see a way to upload entire folders at once so that the organisation remains intact.
The organisation of the folders is as follows:
app>Projects>notes>photos/text/video
Ideally I would like to upload the folder "app" and along with all of its contents while keeping the parent/child structure.
Are you using Google Drive Api for Android or the REST Api ?
If you are using the REST API you can create a folder like this :
private String createFolder() throws IOException {
File fileMetadata = new File();
fileMetadata.setName("FOLDER_NAME");
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = mService.files().create(fileMetadata)
.setFields("id")
.execute();
String Folderid = file.getId();
And then with the file id you do this :
File nFile= new File();
nFile.setName("FILE_NAME");
nFile.setParents(Collections.singletonList(Folderid));
File file = mService.nFile().create(fileMetadata).execute();
The setParents is used to create the file INSIDE the parent which is the folder just created in this example.
I'm new to android development and I am working on a little project. What I am having some issue with is getting access to preloaded files.
In my app, I have an XML file that I preloaded (I just simply put it in my src folder in a package). How do I access them in my classes? I need to get a File object pointing to this file so that I can use it as I would I/O files. It seems like this should be trivial, but alas I am stuck.
Lets say the file is located under: com.app.preloadedFiles/file1.XML
I've tried something along the lines of this, but have had no success:
URL dir_url = ClassLoader.getSystemResource("preloadedFiles/file1.XML");
FIle file = new File(dir_url.toURI());
I solved this in my app by getting an InputStream to the file -- something like:
myContext.getAssets().open(fileName);
//read the data and store it in a variable
Then, if you truly need to do File related opterations with it, you can write it to a private (or public) directory and do your operations from you newly written file. Something like:
File storageDir = myContext.getDir(directoryName, Context.MODE_PRIVATE);
File myFile = new File(storageDir + File.separator + fileName);
//then, write the data to the file and manipulate it -- store the name for access via File later
I need the route of a pdf file in my Android Project.
The file could be in raw or in assets folder.
I need to get the route because I must call using the File Class
Ex: File file = new File(path);
Thanks
Image:
http://img19.imageshack.us/img19/2303/capturaww.png
I am not quite sure what you mean, assuming you want to read to PDF file in your assets (res file) look here
Access PDF files from 'res/raw' or assets folder programmatically to parse with given methods
But if you want to get the file path of a folder, then look here
how to get file path from sd card in android
How to get the file path from URI?
Here is an example of how to get a file path (taken from the third link)
File myFile = new File(uri.toString());
myFile.getAbsolutePath()
I am using this code for creating the new file and write the json object in test.json file but I am unable to create file.Please find mistake and give the best result
One things I have also write the code for permission in manifest file..
Thanks,
String fileName = "test";
File file = new File("/data/data/packagename/test.json");
file.createNewFile();
DataOutputStream fos = new DataOutputStream(openFileOutput(fileName , Context.MODE_PRIVATE));
if(file.exists()){
fos.write(jsString.getBytes());
fos.close();
}
This will not land on C:/ anyway. At best at your SD card or some other folder, and since android is a unix based system. you should be careful on how you use "slashes"