How to Create Files to a Specific Folder in Android Application

How to create files to a specific folder in android application?

use getCacheDir(). It returns the absolute path to the application-specific cache directory on the filesystem. Then you can create your directory

File myDir = new File(getCacheDir(), "folder");
myDir.mkdir();

Please try this maybe helps you.

Ok, If you want to create the TextFile in Specific Folder then You can try to below code.

try {
String rootPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyFolder/";
File root = new File(rootPath);
if (!root.exists()) {
root.mkdirs();
}
File f = new File(rootPath + "mttext.txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();

FileOutputStream out = new FileOutputStream(f);

out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

How to create application specific folder in android gallery?

I guess the proper way of storing your images accessible by galleries is writing the files to this directory.

static final String appDirectoryName = "XYZ";
static final File imageRoot = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), appDirectoryName);

And for videos

static final File videoRoot = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES), appDirectoryName);

And to create an image

imageRoot.mkdirs();
final File image = new File(imageRoot, "image1.jpg");
// open OutputStream and write the file

If you add some files and open the gallery you should see album "XYZ".
Note that you will not see the album if the directory is empty.

And about your words

Currently, I'm able to store the media on sdcard and on scanning the
media files, they show up in "images" folder, in android gallery.

The representation depends on the Gallery app you use.
The default Android gallery should create "Albums" named after the folder the media files are in.

Edit: you will not see newly created image if you don't run media scanner or add it to MediaStore database.

Write a file in internal storage specific folder in Android

you can go this way.

File directory = new File("path_to_directory");
try {
if(!file.exists()) {
directory.createNewFile();
}
File dataFile = new File(directory, "Your File Name");
FileOutputStream stream = new FileOutputStream(dataFile, true); // true if append is required.
stream.write();
stream.flush()
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (null != stream) {
stream.close();
}

Here path_to_directory = Environment.getExternalStorageDirectory() + File.seperator + "FolderName";

OR

path_to_directory = ctx.getFilesDirectory() + File.seperator + "FolderName";

By the way, you cannot create a folder into android's internal storage until it is rooted. It will definitely give you the IOException because without root the android internal FS is read-only. So be aware of that.

Thanks,
Happy Coding :-)

Cannot add file to specific folder location for Android 10

I did not try the following codes, but I used them a while ago in an app:

String path = new File(Environment.getExternalStorageDirectory() + "/My Folder").getPath();
createNewFolder ( path );

The createNewFolder method creates a folder and displays a toast if created, and displays another toast if the folder name already exists:

private void createNewFolder(String path)
{
File file = new File(path);
if (!file.exists()) {
file.mkdir();
Toast.makeText( context , "Folder created" , Toast.LENGTH_SHORT).show();
} else {
Toast.makeText( context , "Folder already exists" , Toast.LENGTH_SHORT).show();
}
}


Related Topics



Leave a reply



Submit