I' am trying to get the location of my Internal Storage dynamically but having issues with it. The following is my code that am currently working with:
Context context = this;
File dir = context.getDir("appdata", Context.MODE_PRIVATE);
File file = new File(dir, "name.txt");
System.out.println( file.toString() );
The return path from the print is /data/data/com.example.application.form/app/name.txt but I want Internal Storage/appdata/name.txt
What am I doing wrong?
use
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
instead
File dir = context.getDir("appdata", Context.MODE_PRIVATE);
Related
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();
}
I am currently working with internal storage. i want to create multiple folders like mainFolder/subFolder/fileName.
i already prepare lot of tutorials and i wouldn't find the best solution.. And i am also getting error java.io.FileNotFoundException: open failed: EISDIR (Is a directory).
My query was
What is this error? any one can please explain this.
How to create folders in internal storage.
Finally I got this One
To create Nested Folders
// Save Internal Storage
File myMainDir = context.getDir("MainFolder", Context.MODE_PRIVATE);
File mySubjectDir = new File(myMainDir, "subFolder");
mySubjectDir.mkdir();
File myModuleDir = new File(mySubjectDir, "semiSubFolder");
myModuleDir.mkdir();
File myFinalDir = new File(mySubjectDir, "fileName.mp4");
// Save External Storage
String DNAME = "MainFolder"+"/"+subFolder+"/"+semiSubFolder;
File rootPath = new File(Environment.getExternalStorageDirectory().toString(), DNAME);
if(!rootPath.exists()) {
rootPath.mkdirs();
}
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.v("Cannot use storage","Cannot use storage");
}
File myFinalDir = new File(rootPath,"fileName.mp4");
Do it like this :
String folder_main = "NewFolder";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
If you wanna create another folder into that :
File f1 = new File(Environment.getExternalStorageDirectory() + "/" + folder_main, "product1");
if (!f1.exists()) {
f1.mkdirs();
}
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
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.
imagedirectory = new File(path);
imagepool = imagedirectory.listFiles();
Uri targetdelete = Uri.fromFile(imagepool[photoindex]); //photoindex is integer 1
File filetodelete = new File(targetdelete);
boolean deleted = filetodelete.delete();
I am receiving an error in this line
File filetodelete = new File(targetdelete);
it says targetdelete must be a string object.... I thought it was valid to put Uri object as the agrument when initializing a File object?
Thanks once again, wonderful experts on stack overflow!!
Why not just do deleted = imagepool[photoindex].delete();