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.
Related
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.
I am trying to create a folder in Drive and using below
String folderName = UrlConstants.APP_NAME + "_dont_delete";
File fileMetadata = new File();
fileMetadata.setTitle(folderName);
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = null;
try {
file = driveService.files().insert(fileMetadata)
.setFields("id")
.execute();
} catch (IOException e) {
e.printStackTrace();
}
However it is not creating folder in drive , instead it is creating a document named 'Untitled'.
Thanks in advance.
In Drive API for Android, note that working with folders has slight differences when compared to Google Drive API. Folders in the Drive API for Android are specialized resources within metadata and a DriveId and to create a folder, call DriveFolder.createFolder for the root folder. Then, pass the metadata containing the title and other attributes to set the values for the folder.
For a full working example, see the CreateFolderActivity sample in the Google Drive Android API Demos app.
The CloudRail SDK allows you to do that in a pretty simple way:
GoogleDrive service = new GoogleDrive(
this,
"[Google Drive Client Identifier]",
"[Google Drive Client Secret]",
"http://localhost:12345/auth",
"someState"
);
service.createFolder(
"/myFolder"
);
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
My application is mostly c++ (using NDK) so I use fopen, fwrite, etc. standard functions to create and game save files and write into them.
When I use fopen("game.sav", "wb"), it appears that it's being created at path
/data/user/10/com.my.game/files/game.sav.
My app is multi-user. So I want to have a separated folders where users store their save-files. And instead of the path above I'd like to have paths like
/data/user/10/com.my.game/files/user0/game.sav,
/data/user/10/com.my.game/files/user1/game.sav, etc
My app's frontend is in Java, and when new user is being registered, I want to create a folder /data/user/10/com.my.game/files/user0/. But I don't know how to do it, because
final File newDir = context.getDir("user0", Context.MODE_PRIVATE);
results in path being created at /data/user/10/com.my.game/app_user0 that's a different path.
It is possible to create folders at /data/user/10/com.my.game/files/ and how ?
Simple way to do it, this code you can change it suit many conditions. If you know that your path is different from what getFilesDir() gets you then you can create a File first of all by using a path that you know and the last 2 lines of code will still be same.
File file = this.getFilesDir(); // this will get you internal directory path
Log.d("BLA BLA", file.getAbsolutePath());
File newfile = new File(file.getAbsolutePath() + "/foo"); // foo is the directory 2 create
newfile.mkdir();
And if you know the path to "files" directory:
File newfile2 = new File("/data/data/com.example.stackoverflow/files" + "/foo2");
newfile2.mkdir();
Both code works.
Proof of Working:
So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];