create visible folder sdcard, Android - android

I'm developping an application where I need to create new folders/files in the sdcard. The thing is I can see them using a root explorer but not with the default one which comes with Android.
I've taken a look at several similar questions here but don't seem to work for me.
For sure, I'm using in my manifest this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and I'm writing all the Files using:
String path = Environment.getExternalStorageDirectory() + "/FolderName/FileName.format"
but as I said, these new folders/files remained hidden and can only be seen using a root explorer. Neither the folder nor file name starts with "."
Thanks in advance.

Try using mkDirs()
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}

Related

How to create folder in root of storage Android

I'm new at this.
Telegram, Whatsapp, Vk, Aliexpress - All this apps can write they folders in /Storage/emulated/0
How can I create my folder in this place for Android 5...10?
When I try to use File("/storage/emulated/0/", myfile) - it doesn't work.
Please, can anyone give some mini example how can I create my files in storage
Since you haven't written any code, I can't tell you what you're doing wrong. But this is something that might help you.
First you need to declare permissions on your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After that you can simply use this to create your folder.
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
"folderName";
File directory = new File(path);
directory.mkdirs(); // It returns a boolean you can use it to check if your folder
// was created.
Yes, You can create Folders inside the External Storage too.
But, keep in mind that you cannot use emulated/0 because it is different for different devices.
File path = new
File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Folder/");
if(!path.exists()){
boolean k = path.mkdirs();
}
Although, Environment.getExternalStorageDirectory() is deprecated as of API29. So You can use getExternalDir() too.

Create a file folder for app

The below code doesn't create a folder in my device.
String intStorageDirectory = context.getFilesDir().toString();
File folder = new File(intStorageDirectory, "test");
folder.createNewFile();;
I need a folder created for my app to store media, when user installs it. That folder should be visible on file explorer. How can i do it?
With the current snippet you created a file, you can also create folder by creating file but your current directory is the base folder, getFilesDir() points internal storage for your app which not visible nor accessible unless explicitly declared. You can create a folder and file by creating with new File().createNewFile() or create only folder using mkdirs() but you won't be able to display it using a file explorer app and that folder and files inside it will be deleted when/if user uninstalls your app.
To save files externally(This doesn't mean saving to SD Card) you can create directory and file with
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName);
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs()
}
File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);
And you need some kind of OutputStream to write data to that file.
Make sure that you ask <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> inside your AndroidManifest.xml file and ask write permission on runtime if your android:targetSdkVersion="23" or above
new File(context.getFielsDir(), "test").mkdirs();
createNewFile creates a file, not a folder. Using mkdirs instead of mkdir ensures that all parents exist. There's also no reason to go through a string when you already have a File.
Adding folder.mkdirs(); should work in place of folder.createNewFile(); And don't forget to add the permissions.
This will create a folder in you data directory.
And just a suggestion , if you want to store media in a SD card folder maybe Environment.getExternalStorageDirectory() is good.

Accessing SD CARD and doing FILE IO Operations

I am trying to create a folder and then after that do some file IO operations!
I am using a sony Xperia Z to test this out!
I know right now I've hardcoded the location but it doesn't let me create folders!
File appPath = new File("/storage/sdcard1/folder");
if (!appPath.exists()) {
appPath.mkdirs();
}
I am using a targetSdkVersion of 22
And having lollipop on my phone.
I tried
appPath.mkDir();
as well but all this gives a value of False.
And i have added permissions to manifest
<uses-permission name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission name="android.permission.READ_EXTERNAL_STORAGE" />
And I tried many different open source file manager but none are able to create folders, But ES file Manager is able to create folders and do File IO operations!
Don't hardcode global paths like /sdcard, use Environment.getExternalStorageDirectory() and related methods instead. Here is a working sample.
public static String getNewFolderPath() {
File folder = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "folder");
if (!folder.exists())
folder.mkdirs();
return folder.getAbsolutePath();
}
EDIT:
For more info check:
Find an external SD card location
#CommonsWare Answer
#Aleadam Answer
Hope it helps!

Make a new folder on internal storage android

So I want to make a folder right in the main part of my internal storage. So basically the same area that you see when you plug your phone in your computer and you open up internal storage and see all those folders and files.
I've tried the following codes:
String path = Environment.getDataDirectory().getAbsolutePath().toString()+ "/storage/emulated/0/appFolder";
File mFolder = new File(path);
if (!mFolder.exists()) {
mFolder.mkdir();
}
Along with
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File myDir = contextWrapper.getFilesDir();
// Documents Path
String documents = "appFolder";
File documentsFolder = new File(myDir, documents);
documentsFolder.mkdirs(); // this line creates data folder at documents directory
Along with a few others I've found online, but none of these seem to make a folder or at least on the internal storage that I want.
I'm not sure if I'm missing something? But any help would be great, I've tried essentially copying a lot of posts I've seen online but none has even gotten me a folder? I don't get any errors either?
I have:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.name.app">
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
in my manifest, although I think I'm using gradle if that helps
-------------edit
Unfortunately I tried this from the url aswell, but still no folder.
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File mydir = contextWrapper.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
Okay somehow fixed the problem
Turns out if you do
File Directory = new File("/sdcard/folderName/");
// have the object build the directory structure, if needed.
Directory.mkdirs();
and use
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
it now works.
I'm not too sure if changing the permission from INTERNAL to EXTERNAL did it, or if it was the directory which is now just /sdcard/folder/or probably both, but either way it worked so I'm happy. Thanks all!!

Create folder in sd card

Alright so I created 2 folders in the SD card using the following code:
String folderPath = Environment.getExternalStorageDirectory() + "/AllAroundMe/Images/";
File file = new File(folderPath);
if(!file.exists())
{
if(file.mkdirs());
Log.d("MyTag","Successfully created folders");
}
I tested this program and it really works, the logcat prints the success message above.
But if I navigate to my sd card I don't see "AllAroundMe" folder.
How can I access that folder from my computer?
Try this
Open DDMS perspective -> File Explorer - > mnt -> sdcard
Go to Android DDMS FIleExplorer-->mnt-->sdcard--> and search for your sdcard folder which is created by you
Just check u can`t give the permission in manifest file in your Application just add the permission of this.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Than run your own code the folder has been create in sdcard ok check this.
First i hope you have given the External Storage permission in your Manifest.xml
Do something like this... i know what you did right, but still i prefer this approach.
File f = new File("/sdcard/AllAroundMe/Images/");
Now browse your sdcard from you pc, i am sure you will find the folder.

Categories

Resources