Directory not creating in internal storage - android

I am trying to create a directory in the internal storage into an Android device using the following code but it seems like I am missing something. The directory is not showing in the internal storage. I have tried this code:
File testDir = DirectoryTestActivity.this.getDir("Test", Context.MODE_PRIVATE);
if (!testDir.exists())
{
testDir.mkdirs();
}

add you internal path in new File(your internal path )
File newFolderVideos = new File(context.getFilesDir().getAbsolutePath() + "/Pictures");
if (!newFolderVideos .exists()) {
newFolderVideos.mkdirs();
}

Related

how to create folder and file in storage directory in android

I wanted to create some folders and some files to /storage/MyFolder/.
I had tried to work on using /storage/emulated/0/MyFolder and it is working but i want to make hide under the storage root folder.
Is there any possible way for me to do this?
You can create folder like this in External Storage directory:
String folder_main = "NewFolder";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
//get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/Abc/";
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
if(!sdIconStorageDir.exists()){
sdIconStorageDir.mkdirs();
}

How can we create a folder of app in device storage in Android?

I'm using code in comment to save a file in directory folder. This code is working properly with mobile phone having sd card.
But if I use it with mobile phone not having sd card it is giving IO exception.
How can I create a folder like whatsapp has in device storage root to save images etc but I want a folder of application name to save .csv files in my device storage root.
First make sure you have added Storage Permissions inside Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If you want to create folder inside ExternalDirectory
File f = new File(Environment.getExternalStorageDirectory()+"/foldername");
if(!f.exists()) f.mkdir();
If you want to create file inside internal storage
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal directory;
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
You can create directory in internal storage and external storage as Follows.
//check whether Sdcard present or not
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
String directory = "/your Directory name or app name ";
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + directory;
File dir = new File(path);
if(!dir.exists()) //check if not created then create the firectory
dir.mkdirs();
//add your files in directory as follows
File file = new File(dir, filename);
}
else
{
// create folder in internal memory
File mydir = context.getDir(directory, Context.MODE_PRIVATE); //Creating an internal directry
if(!mydir.exists()) //check if not created then create the firectory
mydir.mkdirs();
//add your files in directory as follows
File file = new File(mydir, filename);
I hope it will work for you

how to create a folder internal storage in android lollipop

i have tried most of the code i have found on stack over flow read the devlopment documentation but i still fail to create a folder and a file on internal storage in android lollipop i have not tried on lower api but i even tried those internal persmission declaration in manifest.
the below sample of the code i tried do not even work for my situation:
String path = Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
File mFolder = new File(path);
if (!mFolder.exists()) {
mFolder.mkdir();
}
File Directory = new File("/sdcard/myappFolder/");
Directory.mkdirs();
i tried this also below:
File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
documentsFolder.mkdirs();
String publicC = "documents/public/api.txt" ;
File publicFolder = new File(myDir, publicC);
publicFolder.mkdirs();
and then this as well:
ContextWrapper contextWrapper = new ContextWrapper(
getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
First, I recommend you read more on what the following terms mean with respect to Android app development:
internal storage
external storage
removable storage
With that as background, let's review what you did:
Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
Never use getDataDirectory(). I have no idea where this code would point to.
File Directory = new File("/sdcard/myappFolder/");
You do not have arbitrary read/write access to removable storage, and removable storage may not be found at that location anyway.
File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
This code is fine. However, it points to internal storage, and on Android devices, you cannot see internal storage very readily. That too is fine, as developers should be used to the idea that they cannot see everything that their code affects. You might consider writing test cases to confirm that your directory was created.
String publicC = "documents/public/api.txt" ;
File publicFolder = new File(myDir, publicC);
This points to nowhere. Always use some method to derive the base path.
ContextWrapper contextWrapper = new ContextWrapper(
getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
The ContextWrapper is useless. filepath needs to be a simple directory name. You could simplify this as:
File directory = getDir(directoryName, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
Then this code is also fine. It too points to internal storage, and therefore you will not be able to examine it directly. Once again, write test code to confirm that the directory was created as you expect.

Android application file path

So I've built some code to download a file which works fine and I have set it to download into the applications directory which works. it's stored in the application folder /files/dltest
My issue is with checking programatically wether or not the file exists, I've tried methods one stackoverflow and for some reason I can only get my hard coded path to work.
/sdcard/Android/Data/com.test.alihassan.download/files/dltest/REQS.pdf
Using built in methods to retrieve the path gives me the same path but with /data/data/com.... and this doesn't work
File mydir = context.getFilesDir();
File fileWithinMyDir = new File(mydir, "myfile/path/fileNmae");
if(fileWithinMyDir.exists()){
//exists
}else{
//not exists
}
Update:
//File mydir = this.getFilesDir();
File mydir = this.getExternalFilesDir("/dltest/REQS.pdf");
if (mydir.exists()) {
//exists
} else {
//not exists
}

How to create dir on interenal storage if external is unavailable

I am creating a DIR from my app when app first time run.Here is the code for this
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d("MyApp", "No SDCARD");
} else {
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"MyContactsBackUp");
directory.mkdirs();
}
now what i want to know is that if external storage is missing then how can i create DIR on internal storage
In below code you can create directory structure in internal storage:-
File fil = new File(Activitythis.getFilesDir().getPath());

Categories

Resources