To generate a folder first, you need to add permission in AndroidMinifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Now call the following method to create a new folder in which the directories you want to create your own folder (this name must exist in the "Environment" list) and the name of your folder.
File outputDirectory = GetPhotoDirectory(Environment.DIRECTORY_PICTURES, "YourFolder");
Create your folder using this method
public static File GetDirectory(String inWhichFolder, String yourFolderName ) { File outputDirectory = null; String externalStorageState = Environment.getExternalStorageState(); if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) { File pictureDirectory = Environment.getExternalStoragePublicDirectory(inWhichFolder); outputDirectory = new File(pictureDirectory, yourFolderName); if (!outputDirectory.exists()) { if (!outputDirectory.mkdirs()) { Log.e(LogHelper.LogTag, "Failed to create directory: " + outputDirectory.getAbsolutePath()); outputDirectory = null; } } } return outputDirectory; }
If you want to create a file under your newly created folder, you can use the code below
public static Uri GenerateTimeStampPhotoFileUri(File outputDirectory, String fileName){ Uri photoFileUri = null; if(outputDirectory!=null) { File photoFile = new File(outputDirectory, fileName); photoFileUri = Uri.fromFile(photoFile); } return photoFileUri; }
A call to create a file after creating a folder with a file directory. It will return your Uri file
Uri fileUri = GenerateTimeStampPhotoFileUri(File outputDirectory);
source share