How do I create a folder in internal storage? - java

I want to create a folder for my app to put audio files into. I want it to be in the same location where the DCIM folder lives. So when I run the emulator and go to Files -> Internal Storage, I want to be able to see the folder here (similar to how Snapchat creates its folder here). Is this even possible? I tried doing the below but got an error.
Code:
var path = Environment.getExternalStorageDirectory() // this is /storage/emulated/0
val testappDir = File(path?.absolutePath, "/testapp/")
testappDir.mkdirs() //mkdirs creates any missing parent directories, mkkdir does not
if (testappDir.createNewFile()) {
println("File created: " + testappDir.name)
} else {
println("File already exists.")
}
Error:
java.io.IOException: Operation not permitted

You are calling a path for an external storage with Environment.getExternalStorageDirectory().
The Exception java.io.IOException: Operation not permitted means that you don't have the permissions to read or write the external storage.
You have to add the permission to the Android Manifest and the at Runtime you have to request the permission from the user. More information about this can be found here: https://developer.android.com/training/permissions/requesting
When you want to read and write the App Internal Storage you can found more information here: https://developer.android.com/training/data-storage/app-specific

Related

Unable to get files from folder with .nomedia file or hidden folder in Android 11

I want to get all files from WhatsApp's .Statuses folder. Until Android 10 im perfectly getting all statuses files. But on Android 11 due to new restrictions, when I code like below:
File(Environment.getExternalStorageDirectory().absolutePath + File.separator + "Android/media/com.whatsapp/WhatsApp/Media/.Statuses").listFiles()
I always get 0 files. Whereas, Im successfully getting other folder files "Android/media/com.whatsapp/WhatsApp/Media/" on this path.
Two problems I'm facing now:
If a folder is hidden then in Android 11, listFiles() returns 0 on that folder.
If a folder not hidden but contains one file as ".nomedia" , listFiles() returns 0 on that folder as well in Android 11.
What should I do to get all whatsapp statuses files in Android 11?
I dont want to use MANAGE_EXTERNAL_STORAGE permission for it due to google policies. Thank you
There are ways to access Hidden Directory in Android 11, two of them are:
Storage access framework (SAF) in which you take user to that specific directory and ask for permission from user to access directory files and that way you get access to files in it (Study SAF).
You can use File Observer on that hidden folder and whenever any file created or modified or deleted from that hidden directory you will get full path in that case for that specific file in hidden folder & once you have full path you can have access to that file.

How can I find the main directory on my Android app?

I would like my Android app to find the main directory on my tablet's internal storage. The furthest I can get is reading the contents of the "user.dir", but that seems to be the root directory and the listing there is too immense to find what would be considered the DOS equivalent of C:\
I am listing the code snippet I use to read the directory to show I can access the internal storage, but somewhere I'm not able to do it correctly.
workingDir = System.getProperty("user.dir");
File f = new File(workingDir);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
Thanks
in fact, if you were to open the "My Files" explorer that comes with Android the top option on my tablet is called Internal Storage, and it lists all the viewable folders from within
That is what the Android SDK refers to as external storage. You can find the root of that via Environment.getExternalStorageDirectory(). However:
Don't put things directly there, just as well-written Windows programs haven't put stuff in C: for the past couple of decades. Use Environment.getExternalStoragePublicDirectory() for standard locations (e.g., downloads directory), or use getExternalFilesDir() on Context for a location that is unique to your app.
You need to hold READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE permissions, including setting up runtime permissions for use on Android 6.0+ devices.
You can try to give a look at the android Environment documentation and access internal storage root/main directory with this Environment.getRootDirectory()

android - retrieve all files from /res/raw folder as File type

How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)

Creating a directory and folder in the internal storage using android studio

I have read online that to make a directory in the internal storage we use the following line of code
File dir = getDir("mydir", Context.MODE_PRIVATE);
however, when I execute this code along with my entire source code, I can't seem to find the directory I created in my phone. Isn't it supposed to be saved into the android/data directory where all other apps data is stored? I am slightly confused.
private file stored in
/data/data/<packagename>/files

Create folders programmatically along with permissions using java to save content to that location

I have xampp installed in my windows system and lampp installed in my linux system. I want to create folder in the location "http://localhost/" using java. I have done the following :
dirName = new File("http://localhost/"+name);
if(!dirName.exists()) {
dirName.mkdir();
}
Is it possible to do? The idea is to download some files to that location programatically. Download is working but how do I create folders so that I can access it via http://example.com/name. This is required to keep track of the user related content. I have access to the apache web server with lampp already installed. How can I create folders and save the downloads to that folder with programmatically assigning the permissions to the folder and contents within it so that saved contents can be download from there using wget method.
Don't use the File API. It is ridden with misbehavior for serious filesystem work.
For instance, if a directory creation fails, the .mkdir() method returns... A boolean! No exception is thrown.
Use Files instead.
For instance, to create a directory:
// Throws exception on failure
Files.createDirectory(Paths.get("/the/path"),
PosixFilePermissions.asFileAttribute(
PosixFilePermissions.fromString("rwxr-x---")
));
Use Java Files with PosixPermission.
[Note- PosixPermission is not supported in Windows]
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwxrwx");
Files.createDirectories(path, PosixFilePermissions.asFileAttribute(perms));
In Java you can create files in any writeable directory on your system by doing something like:
File file1 = new File("/var/www/newDirectory/");
file1.mkdirs();
Then to create a file in that directory you can do something like this:
File file2 = new File(file1.getAbsolutePath() + "newFile.txt"); // You may need to add a "File.seperator()" after the "file1.getAbsolutePath()" if the trailing "/" isn't included
if (file2.exists() == false) {
file2.createNewFile();
}
To ensure that your file is readable to the public you should add read permissions to the file:
file2.setReadable(true, false);
In Apache you can set up a virtual host that points to the directory where you would like to make files available from. By default on debian linux it is /var/www.

Categories

Resources