How to save folder in specific folder in Google Drive - android

I can save/copy a file in any folder of Google Drive using the below code
.But when i apply this method to save the folder in any other folder, then it can't save folder, and nothing happens. I want to copy a folder and save it at another place
String LocationID="0B-WFTScd2afSFS34TC-223";//getting the id of the parent folder where we have to make the new file as its child
File orgnlFile =global_file;
File copiedFile = new File();
copiedFile.setTitle(orgnlFile.getTitle());
copiedFile.setMimeType(orgnlFile.getMimeType());
copiedFile.setParents(Arrays.asList(new ParentReference().setId(LocationID)));
try
{
mService.files().copy(orgnlFile.getId(), copiedFile).execute();
} catch (IOException e)
{
System.out.println("An error occurred: " + e);
}
But unfortunately it doesn't work for copy-paste folder in some other directory

This is because copy is duplicating the media contents of a file into a new file. Since a folder has no media contents, there is nothing to copy.
When you say "I want to copy a folder", do you mean (1) copy only the folder, or (2) copy the folder and all files within the folder, and do you mean (3) duplicate or simply make a second reference?
Depending on exactly what you want to achieve, the Drive API calls you need to make are different.
Perhaps the best way to explain what you want would be to express it in terms of the *nix commands "cp", "mv" and "ln"
To create a duplicate of a folder-a (which is under folder-b) and its children within folder-c, (in *nix, "cp -R /folder-b/folder-a /folder-c") you will need to :-
Make a new folder-a under the new parent folder (folder-c). NB although folder-a has the same name as the original. It is a different folder.
Make a note of the file-id of the new folder-a
Get a list of the children of the original folder-a (children feed)
For each child, copy it
Update the parents property of each new file to be the new folder-a
If there are folders within folder-a, you will need to do the above recursively

Related

How to get path to file in android apk

My app depends on 3 different files. I already put the two larger ones as expansion files which seems to work fine. Now I don't know where to put the third one (small file). I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
I tried this here for the raw folder
myClassifier.loadModel( pathToExpansionFiles + "/File1",
pathToExpansionFiles + "/File2",
R.raw.File3);
However the return value of R.raw.File3 is an integer, but the function myClassifier expects a string that is the path to File3. Has anyone an idea how to do that?
I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
There is no path to it. That is a file on your development machine. It is not a file on the device. It is merely an entry inside your APK file.
but the function myClassifier expects a string that is the path to File3
Either switch to some library that works with an InputStream (from getResources().openRawResource()) or make a copy of the data to a local file using that InputStream yourself.

Error creating directory with specific name android

I need to create 2 directories in my android application,
I can use this code to create a directory just fine
File m_Dir = new File(getFilesDir() + "/randomDir");
m_Dir.mkdir();
if (m_Dir.isDirectory()) {
Log.w("tag","randomDir dir is a directory");
}
however I cannot use this because it does not create a directory, no errors, but it just isn't a directory for some reason, even though the only thing that changed is the name of the file
File m_Dir = new File(getFilesDir() + "/dict");
m_Dir.mkdir();
if (m_Dir.isDirectory()) {
Log.w("tag","dict dir is a directory");
}
note they must be named "dict and "dbfiles", Using them for JWI wordnet, and thats how it locates the files it needs.
I have tried this many times with different names, and the only reason that this m_Dir.isDirectory returns false is because of the name, this doesn't make sense to me. The only reason I can think of, that this might fail is because I have file directories in my Assets folder with the same name.(I am copying these into my new directory so i can use them). I can change the name of the Asset folders (Probably what I'm going to do). However I created another folder in assets using a different name, I tried that name and it worked creating the directory using the same code as before. So that may not even be the problem. I was wondering wondering why this problem is occurring (No errors but no directory) and if there is any better way around it?

android data save directories

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:

Slashes removed from calls to File.mkdirs()

As the title suggests, I am trying to create a folder on Android, but all of the slashes have been removed from it.
For some more background information:
Specifically, I am trying to create a directory to store my application's users' files. These files must be accessible to the user from a file manager (such as File Manager HD) because the application does not support full file management. Using the standard from API level 8+, I reference the root of the publicly accessible folder with Environment.getExternalStoragePublicDirectory(). I then try to create a folder located at DCIM > Sketchbook > [the name of the sketch] using File.mkdirs(). For more information, see the code below.
I have already:
checked to make sure that the SD card is mounted, readable, and writable
enabled the permission WRITE_EXTERNAL_STORAGE
tried using File.mkdir() for every file in the hierarchy up to the folder location
tried using /, \\, File.separatorChar, and File.separator as folder separators
Code:
boolean success = true;
//The public directory
File publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
//The location of the sketchbook
File sketchbookLoc = new File(publicDir + "Sketchbook" + File.separator);
//The location of the sketch
//getGlobalState().getSketchName() returns the name of the sketch: "sketch"
File sketchLoc = new File(sketchbookLoc + getGlobalState().getSketchName() + File.separator);
if(!sketchLoc.mkdirs()) success = false;
//Notify the user of whether or not the sketch has been saved properly
if(success)
((TextView) findViewById(R.id.message)).setText(getResources().getText(R.string.sketch_saved));
else
((TextView) findViewById(R.id.message)).setText(getResources().getText(R.string.sketch_save_failure));
With various incarnations of the aforementioned tests (the ones that actually worked), I have received a consistent result: I get a new folder in DCIM whose name corresponds to the combination of all of the folders that should have been hierarchical parents of it. In other words, I have created a new directory, but all of the folder separators have been removed from it.
Now, I ask you:
Am I attempting to save the user data in the correct location? Is there another way that I should be doing this?
Is it even possible to create new folders in the DCIM folder? Does Android prevent it?
Is this problem specific to me? Is anyone else able to create a folder in the DCIM folder?
Am I using the right folder separators?
Is there something else that I am absolutely, completely, and utterly missing?
Now that I am done typing, and you are done reading my (excessively long) question, I hope that I can find some sort of answer. If you need clarification or more information, please say so.
EDIT: An example of the created folder is "DCIMSketchbooksketch", where it should be "DCIM/Sketchbook/sketch".
don't use
File sketchbookLoc = new File(publicDir + "Sketchbook" + File.separator);
but
File sketchbookLoc = new File(publicDir , "Sketchbook");
because publicDir.toString() will not end with a file separator (even if you declared it that way). toString() gives the canonical name of the file.
So your source becomes :
//The location of the sketchbook
File sketchbookLoc = new File(publicDir , "Sketchbook" );
//The location of the sketch
File sketchLoc = new File(sketchbookLoc , getGlobalState().getSketchName() );

On Android Context.getCacheDir() seems to return a directory I cannot create subdirectories in

Does Android not allow creating subdirectories in the cacheDir? Given that third party (such as advendors) surely will need to cache their own files, it makes lots of sense for me to create a subdirectory for my cached files and let third parties I've integrated manage their own cached files.
Does anyone know how to set the permissions so you can create subdirectories?
Ex. the following code appears to always fail to create a subdirectory.
File cacheDir = context.getCacheDir();
File subDir = new File(cacheDir, "subdir");
if ( !subDir.exists() ) {
if ( !subDir.mkdirs() )
Log.w(TAG, "Failed to create subdirectory in cache");
}
I just tried, I created subfolder successfully inside the cache folder.
I'm assuming maybe you've gotten a folder with the same name, maybe that's why you failed to mkdir?

Categories

Resources