Saturday, 1 October 2011

Check and Create Directory inside SD Card


                     

         If you need to access the SD card means,First you have to get this permission,

         <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">            </uses-permission>


in Android Manifest. XML file.

Here the Code to Create a Directory inside SD Card



public class CheckAndCreateDir extends Activity {
/** Called when the activity is first created. */

      @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File file = null;

try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File sdCard = Environment.getExternalStorageDirectory(); //getting sdcard content
File check = new File(Environment.getExternalStorageDirectory()
+ "/your own Director name"); //getting our own directory path
if (check.exists()) { //checking our own directory is present or not
if (check.isDirectory()) { //checking that path is directory or not
File[] list = check.listFiles(); //getting list of files inside the directory
int size = list.length;
if (size == 0) {
file = new File(check, "Android");
file.createNewFile(); //Creating a new file inside our directory
FileOutputStream f = new FileOutputStream(file);
f.write("Hai This is First Text".getBytes());  //writing value inside that file
f.close();
Log.v("Created","=======>>Created Successfully<<=======");
} else {
FileWriter file_writer = new FileWriter(check+ "/Android", true);
BufferedWriter Buffered_writer = new BufferedWriter(file_writer);
Buffered_writer.write(" This is Second Text");
Buffered_writer.close();
Log.v("Append","=======>>Appended Successfully<<=======");
}
}
} else {
System.out.println("===========Directory Not Present=========");
check.mkdirs();
}


}

} catch (Exception e) {
}
}
}


After run this application,Change the mode of USB debugging from charge the phone to disk drive.

Then Check the folder is present or not in removable disk.


You can see below Screen inside the folder.



1 comment: