Thursday, November 12, 2020

How to Create Folder in Android

How to Create Folder in Android

External storage is a secondary memory/sdcard of your phone, which we can use to save files world-readable. We can use the mkdirs() method to create the folder in Android.

How to Create Folder in Android

Function

public String getStorageDir(String fileName) {

    //create folder

    File file = new File(Environment.getExternalStorageDirectory() + "/folderName/folderName1");

    if (!file.mkdirs()) {

        file.mkdirs();

    }

    String filePath = file.getAbsolutePath() + File.separator + fileName;

    return filePath;

}

To read or write to the external storage (sdcard), you need to add the permission code in the manifest file

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

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

No comments:

Post a Comment