Android create a folder

When you create Android applications that require to use external storage of the phone to store files, you will need to create folders in the phone' external storage. In Android, you can get the path of the external storage by using the getExternalStorageDirectory() method of the Environment class. Then you can use the mkdir() or mkdirs() method of the File class to create a folder in the external storage. If the parent folder of the folder to be created already exists, you will use the mkdir() method. Otherwise, you must use the mkdirs() method. The mkdir() method will only create the bottom most folder. Your folder won't be created if its parent folder doesn't exist in the directory structure. You can use the exists() method of the File class to check whether the folder to be created already exists before calling the mkdir() or mkdirs() method. It returns true if the folder already exists. Otherwise it returns false. The mkdir() or mkdirs() method returns true if the folder is successfully created. Otherwise, it returns false.



package com.example.andtip;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {

   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
        }


   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.main, menu);
  

return true;
    
   }
  
   public void onStart(){
   super.onStart();
   createFolder("doc_download");
   }
   public void createFolder(String fname){
   String myfolder=Environment.getExternalStorageDirectory()+"/"+fname;
   File f=new File(myfolder);
   if(!f.exists())
      if(!f.mkdir()){
      Toast.makeText(this, myfolder+" can't be created.", Toast.LENGTH_SHORT).show();

}
else
   Toast.makeText(this, myfolder+" can be created.", Toast.LENGTH_SHORT).show();
else
   Toast.makeText(this, myfolder+" already exits.", Toast.LENGTH_SHORT).show();
   }

  
}

If you want to create a folder in the internal memory of the phone, you will use the getFilesDir() method rather than the getExternalStorageDirectory() method. The getFilesDir() will get the folder data/data/your_package_name/files in internal memory.

File folder = getFilesDir();
File f= new File(folder, "doc_download");
f.mkdir();

Posted by: Dara | post date: 05-27-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.