Creating Directories [Solved]

Put your problem here if it does not fit any of the other categories.

Creating Directories [Solved]

Postby izu516 » Sat Nov 14, 2009 2:41 am

This is the code I use to create directories:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. File theThing = new File(currentDirectory + "/" + value);
  3.  
  4. itt = theThing.mkdirs();
  5.  
  6. if (itt == true){
  7.  
  8.         Log.d("myapp", theThing.getPath().toString() + " success");
  9.  
  10. }else{
  11.  
  12.         Log.d("myapp", theThing.getPath().toString() + " fail");
  13.  
  14. }
  15.  
  16.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


It always fails.
Some Info: currentDirectory is usually something like '/sdcard/Folder' (and Folder already exists), and value is user input.

I know the directory is correct because changing the code from theThing.mkdirs() to theThing.createNewFile() creates a blank file in the correct folder. I have also tried changing 'theThing.mkdirs()' to 'theThing.mkdir()' to no avail.

So I just want some help on creating directories within the SD Card. I've researched (how I got my current code), but most topics dealt with creating them on the internal file system. Thanks in advance.
Last edited by izu516 on Tue Nov 17, 2009 11:42 pm, edited 1 time in total.
Go anddev!
izu516
Developer
Developer
 
Posts: 27
Joined: Sat Nov 14, 2009 2:18 am
Location: US

Top

Postby Kur1310 » Tue Nov 17, 2009 7:36 am

u can execute the mkdir command of linux as follows:

[syntax="java"]

String currentDirectory = "sdcard/Folder" , value = "";
String[] command = { "mkdir",currentDirectory+"/"+value};
try {
Process process = Runtime.getRuntime().exec(command);

}catch (IOException e) {
Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
}

[/syntex]

This way you can create directories....
Hope this is the solution u wanted.
-Kur
Kur1310
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Oct 28, 2009 10:50 am

Postby izu516 » Tue Nov 17, 2009 1:45 pm

Thanks. I'll be sure to try this when I get to my computer and reply with the result.
Go anddev!
izu516
Developer
Developer
 
Posts: 27
Joined: Sat Nov 14, 2009 2:18 am
Location: US

Postby izu516 » Tue Nov 17, 2009 2:05 pm

Nothing really happens. A folder isn't made, but no errors occur. Am I supposed to launch the process created in some way, or does just creating the process variable execute it? I'm sure the folder isn't already present because I tried with multiple names.
Go anddev!
izu516
Developer
Developer
 
Posts: 27
Joined: Sat Nov 14, 2009 2:18 am
Location: US

Postby Kur1310 » Tue Nov 17, 2009 2:25 pm

No u only need to create a variable of process, get the runtime and execute the command..
It should work,because i've tried it and it worked....
Firstly, do u have Folder directory in sdcard and have u pass any name in the value variable....
If all is done... then this code should work......
-Kur
Kur1310
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Oct 28, 2009 10:50 am

Postby izu516 » Tue Nov 17, 2009 11:17 pm

Well, I modified the code for error checking.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. try {
  3.  
  4.         String aDirectory = "/sdcard/Folder" , aValue = "itworks";
  5.  
  6.         String[] command = { "mkdir",aDirectory+"/"+aValue};
  7.  
  8.         Process process = Runtime.getRuntime().exec(command);
  9.  
  10.         Runtime.getRuntime().exec(command);
  11.  
  12.         Toast.makeText(this, "success", Toast.LENGTH_LONG).show();
  13.  
  14. }catch (IOException e) {
  15.  
  16.         Toast.makeText(this, "failed "+e, Toast.LENGTH_LONG).show();
  17.  
  18. }
  19.  
  20.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


As it turns out, the toast returns 'success', but no folder is created. I looked it up and hear that there is supposed to be a new api specifically for making directories. Or maybe I am missing a permission? As I'm testing this on a G1 and an HTC Hero, and Folder does exist, I'm very baffled. Oh, and thanks for your help so far.
Go anddev!
izu516
Developer
Developer
 
Posts: 27
Joined: Sat Nov 14, 2009 2:18 am
Location: US

Top

Postby izu516 » Tue Nov 17, 2009 11:36 pm

Well, thanks. Apparently, my G1's space was full and I accidentally cleared the notification, therefore, it couldn't create a new folder. When I tried this on my Hero (which I haven't been using for development since the beginning of the project), it worked flawlessly. While I still wonder why the code didn't just fail, it works now. I am going to post this code over at Code Snippets (giving credit to you) just because it might not be immediately obvious. Thanks again.
Go anddev!
izu516
Developer
Developer
 
Posts: 27
Joined: Sat Nov 14, 2009 2:18 am
Location: US

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 11 guests