Friday 8 November 2013

Android : Create New Folder in External Storage Directly.

In this tutorial we will create folder directly in external directory or at root.
follow Below steps.

1. Create New Android Application Project. Name it as RootFolder.
2. Open Your MainActivity.java file.
3. Replace your MainActivity.java code with below code.

MainActivity.java


package arshad.rootfolder;

import java.io.File;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                               
                                 String newFolder = "/arshad";//folder name
                                    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                                    File myNewFolder = new File(extStorageDirectory + newFolder);
                                    myNewFolder.mkdir();
                }

                @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;
                }

}

4. Now Replace Below Code With your AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="arshad.rootfolder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="arshad.rootfolder.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
 
5. Now Compile And Run Your Project. 
check your your external directory to new folder created or not.

To create new folder in external storage you may use below code anywhere you want.

 String newFolder = "/arshad";//folder name
                                    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                                    File myNewFolder = new File(extStorageDirectory + newFolder);
                                    myNewFolder.mkdir();

to take permission add this line to your AndroidMenifest.xml

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

All The Best :)



No comments:

Post a Comment