-6

Here is a code that copy asset to SD card:

However, it only copy file to the root directory. If I change the code to this, then the file will be copy to mydirectory, but the condition is that "mydirectory" must be first created manually:

Environment.getExternalStorageDirectory() + "/mydirectory/"
                                    + files[i]);

My question is how should I modify the code so that a new directory can be created? I've found this and came across creating file object and use mkdirs()

However I have really no idea on how to do it since I'm new in android development and do not have prior knowledge in programming.

I would appreciate if somebody can give me step by step guide on how to implement this.

Thank you.

Community
  • 1
  • 1
user2190227
  • 23
  • 1
  • 7
  • You already have an answer in the link that you have provided. – Kumar Bibek Mar 21 '13 at 13:30
  • http://stackoverflow.com/questions/15517247/how-to-create-a-directory-in-external-sd-in-android-application/15517312#15517312. Check this link. – Raghunandan Mar 21 '13 at 13:50
  • Yes, I know the link in provided have the answer, but the problem is I do not know how to implement it without step by step guide. I'm know nothing about programming. Hope you can help. – user2190227 Mar 21 '13 at 14:49

2 Answers2

1

You have to create the directory before you start adding files (obviously).

Use this in the file where you try to copy the so called "assets". See the comments:

String filename = Environment.getExternalStorageDirectory() + "/mydirectory/"); // this sets the filename of the new dir
File newDir = new File (filename); // this creates the File object (no directory yet)
if (!newDir.exists ())  // here we check whether the dir is already there
    newDir.mkdirs ()   // if not, then create all necessary directories and subdirectories to our new dir 
Shade
  • 9,936
  • 5
  • 60
  • 85
  • I can simply put File newDir = new File (filename); under the above code? There is an error after I put it, which is filename can not be resolved to a variable? I learn android development by self learn. I'm really appreciate if you can provide step by step guide. – user2190227 Mar 21 '13 at 13:37
  • No, see that I added a declaration to your code. Also, I didn't finish the post last time. See my edit above. – Shade Mar 21 '13 at 14:15
  • I would really appreciate if you can tell me the full step that I need to take for this. For instance where should I put your code? What should be added? – user2190227 Mar 21 '13 at 17:08
  • See my edited answer above. I also added comments. You basically put this code immediately before your other code. – Shade Mar 21 '13 at 18:00
  • Thank you. I know the answer you provided is working, but I don't have enough knowledge to do it myself. Probably I'll learn the complete android development starting from the basic in the future. – user2190227 Mar 23 '13 at 12:18
0

it might help you..

boolean success = (new File("/sdcard/dirname")).mkdir(); 
if (!success)
{
    Log.w("directory not created", "directory not created");
}
Ajay
  • 1,189
  • 1
  • 12
  • 28