Creating a Folder
Copy Code code as follows:
File File = Environment.getexternalstoragedirectory ();
File File_0 = new file (file, "File_demo");
if (!file_0.exists ()) {
File_0.mkdirs ();
}
When you create a folder, you need to <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> Permissions,
Otherwise, you will report the following error:
ApplicationContext Unable to create external files directory
It is recommended that you create a folder using Mkdirs () instead of mkdir (), because the former can create the parent folder at the same time, if it does not, the latter cannot.
Creation of files
Copy Code code as follows:
File File = Environment.getexternalstoragedirectory ();
File File_0 = new file (file, "pic");
if (!file_0.exists ()) {
File_0.mkdirs ();
}
try {
File pic = new file (file_0, "pic.png");
InputStream is = Getresources (). Openrawresource (
R.drawable.ic_launcher);
OutputStream OS = new FileOutputStream (pic);
byte[] data = new byte[is.available ()];
Is.read (data);
Os.write (data);
Is.close ();
Os.close ();
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
The file name you create cannot have a. suffix, or you will report the following error:
Java.io.filenotfoundexception:/mnt/sdcard/pic/pic.png (is directory)
Also, it is a good idea to add the following permissions when reading and writing to a folder:
Copy Code code as follows:
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>