Save file in specific folder with Google Drive SDK - java

I've been trying to save a plain text file into a specific folder in Google Drive on Android.
So far using the Documentation and QuickStart Guide on Google Drive I have been able to do a few things that are going in the right direction, first I was able to create a plain text file:
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
File file = service.files().insert(body, textContent).execute();
I have been able to create a new folder in the base directory of Google Drive with:
File body = new File();
body.setTitle("Air Note");
body.setMimeType("application/vnd.google-apps.folder");
File file = service.files().insert(body).execute();
I have also been able to list all of the folders in the user's Google Drive account with:
List<File> files = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
for (File f : files) {
System.out.println(f.getTitle() + ", " + f.getMimeType());
}
However I am a bit stuck on how to save a text file into a folder within Google Drive.

You need to use the parent parameter to put a file in a folder using insert. More details at https://developers.google.com/drive/v2/reference/files/insert
Something like this
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new File.ParentReference().setId(parentId));
File file = service.files().insert(body, textContent).execute();

If you want to INSERT a file in specific folder in Google Drive then follow these steps. Lets assume that we have retrieved all folder from the Drive and now i will INSERT an empty file in the first folder from the list, So
//Getting Folders from the DRIVE
List<File> files = mService.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
File f =files.get(1)//getting first file from the folder list
body.setTitle("MyEmptyFile");
body.setMimeType("image/jpeg");
body.setParents(Arrays.asList(new ParentReference().setId(f.getId())));
com.google.api.services.drive.model.File file = mService.files().insert(body).execute();
Now this will create an empty file in the Folder which are at the top in the retrieve file list.

Step1 : Create a folder
File body1 = new File();
body1.setTitle("cloudbox");
body1.setMimeType("application/vnd.google-apps.folder");
File file1 = service.files().insert(body1).execute();
Step2 : Insert your file
File body2 = new File();
body2.setTitle(fileContent.getName());
body2.setMimeType("text/plain");
body2.setParents(Arrays.asList(new ParentReference().setId(file1.getId())));
File file2 = service.files().insert(body2, mediaContent).execute();

Related

Replacing files in multilevel zip folders

My requirement is to replace few files in a zip file.
The zip file in turn have multiple zip files and folder within it. It goes upto 4 or more levels of zip files.
I have a set of files in a different source directory. I would like to copy these files and replace within the zip file, by matching the file name in the source directory with file name inside the zip file.
Could someone help here please.
Thanks,
Deleep
One way is use temp files to store the ZIPs and open them as FileSystems, so you dont need to extrat all files and rezip everything
FileSystem firstZip = FileSystems.newFileSystem(URI.create("jar:file:/root/firstZip.zip"),
new HashMap<>(), null);
//Get a zip inside the first one
Path path = firstZip.getPath("FOLDER/ZIP_NAME.zip");
//Get the second zip input stream and store the zip in a temp file
InputStream in = firstZip.provider().newInputStream(path);
Path secondPath = Paths.get("/root/temp/tempFile.tmp");
Files.copy(in, secondPath);
//open the second zip
FileSystem secondZip = FileSystems.newFileSystem(new URI("jar:" + secondPath.toUri().toString()),
new HashMap<>(), null);
//iterate files in the second zip
DirectoryStream<Path> ds = Files.newDirectoryStream(secondPath);
for(Path p: ds){
//something
}
//delete or update files inside the second zip
//Files.delete(seconZip.getPath("aFile.txt"));
//Files.copy(anInputStream, secondZip.getPath("destFoldet/aFile.txt"));
//clse the seconzip
secondZip.close();
//update the second zip inside first one
Files.copy(secondPath, firstZip.provider().newOutputStream(path));
//delete temp file
Files.delete(secondPath);
//close first zip
firstZip.close();
Other idea is jboss-vfs. Never tried it, but i think that could help.

oreilly multipartrequest class to upload multiple images

i am using oreilly multipartrequest class to upload multiple images, images are getting uploaded fine , but i want to know the name of all the images uploaded per request.
i am trying to print all filename using the below but it is printing the name of the last file only. I am trying to upload files 1.jpg , 2.jpg , 3.jpg , but it is printing only 3.jpg , what should i modify in the code to make it print all names.:
MultipartRequest m = new MultipartRequest(request, "C:/Users/;
Enumeration files = m.getFileNames();
while (files.hasMoreElements()) {
String name = (String) files.nextElement();
filename = m.getFilesystemName(name);
System.out.println(filename);
}
You can also list the files by making a file with the directory you wish to list.
File dir = new File(directory);
for (File f : dir.listFiles()){
System.out.print(f.getName());
}
Hope this helps
For more refernce check this link:
Oracle reference on File class

creating folders for all users

I am trying to create a folder by running java. As of now I have this and it works.
File f = new File ("/Users/myName/Desktop/nameOfDir");
f.mkdirs();
The question is what happens when I send this code to my friend? Would he have to change the code to the below for it to work?
File f = new File ("/Users/myFriendsName/Desktop/nameOfDir");
f.mkdirs();
how can I make the program find the correct path and create the folder where I want it (the desktop), regardless of who the user is?
Also, after creating the folder I will have to create a .txt in the folder. I can do this now, but same problem arise concerning different user names.
try using the following:
String userName = System.getProperty("user.name"); //platform independent
File f = new File ("/Users/" + userName + "/Desktop/nameOfDir");
f.mkdirs();
You can get the path to a user's home directory in a platform-independent way using:
System.getProperty("user.home");
So:
File f = new File(System.getProperty("user.home"), "Desktop/nameOfDir");
f.mkdirs();

Drive Api Phone

I'm trying to create a java program that will create and upload excel files into a google drive. After uploading I need it to give permissions. I have done all thatbut the problem lies in trying to convert the excel files into a google files in order that people who use the sheets are able to update them with their phones. I have not been able to find any help on how to do this on the google api. Any help will be appreciated.
Assuming you have the path to your Excel file:
java.io.File fileContent = new java.io.File(excelfilepath);
FileContent mediaContent = new FileContent("application/vnd.ms-excel", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("application/vnd.ms-excel");
// THIS IS THE IMPORTANT PART
Insert request = service.files().insert(body, mediaContent);
request.setConvert(true);
File file = request.execute();
// END OF THE IMPORTANT PART
if (file != null) {
showToast("Data Uploaded: " + file.getTitle());
}

Creating a series of Folders in sdcard android

My require file in sd card has path
Android/data/mypakagename/android/myfile/abc/myfile.txt
i have a url from where i will download this myFile.txt and place it in this location.
Right now I have directory Android/data/mypakagename/ created in sdCard.
Now i am using following code.
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
file.mkdirs();
This creates all folders however it also create myfile.txt as a folder.I want to create all missing directories except last one. How can i do that?
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
File parent = file.getParentFile();
parent.mkdirs();
String filename = "myfile.txt"
File dir = new File ("Android/data/mypakagename/android/myfile/abc");
dir.mkdirs();
File file = new File(dir, filename);

Categories