I tried this link:
How to create text file and insert data to that file on Android
But, it says "No such file or directory". Can anybody help me please? Thanks in advance!
---- Updated answer----
I think you can't create a directory inside the internal storage of the device. Except you've a root access for the app. You can only create the directory inside your app private folder within the following path String path = getFilesDir().
you can use like this below -
File mydir = context.getDir("mydirectory", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myAwesomeFile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
getDir(StringName, int mode) method to create or access directories in internal storage.
/storage/emulated/0/Notes/ will always return No such file or directory except device is rooted and dir.mkDirs() will always return false for this path.
Hope this will help you.
Related
I have the code bellow:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/data/com.example.stavr.mydiary";
File file=new File(path);
File[] files = file.listFiles();
String arr[]=file.list();
List l=new ArrayList<>();
for(String i:arr){
if(i.endsWith(". txt")){
l.add(i);
}
}
which returns 0 files on my array (NullPointerException to be exact), Iknow that there are files in that directory. If i change the directory to just
Environment.getExternalStorageDirectory().getAbsolutePath()
the code work and it returns all the files. I thing that i have done something wrong with the permissions. Could you please help me,
Thank you very much in advance..!!!
It seems you're trying to access your internal app data folder, you can do that by using a context object:
String dir = context.getFilesDir().getAbsolutePath();
If you still want to access external storage then you should make sureyou have WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml
I have writing a program which ask for video file from user. once video is browser by user it is save inside war/content. I do not have any problem to do this. when i want to see the video, i used video tag of html and give the relative path. video come perfectly fine and it can be viewed.
I used the following line of code to resolve the path.
System.setProperty("webapp.root", "C:/solution/totalsolution");
File webappDir = new File(System.getProperty("webapp.root", "./"));
File webappInfo = new File(webappDir, "war");
if(!webappInfo.exists()){
File dir = new File(webappDir, "war");
if(!dir.exists()){
if(dir.mkdir()){
webappDir = dir;
}
}
}
webappDir = webappInfo;
webappInfo = new File(webappDir, "content");
if(!webappInfo.exists()){
File dir = new File(webappDir, "content");
if(!dir.exists() && dir.mkdir()){
webappDir = dir;
}
}
webappDir = webappInfo;
if(contentDir != null){
webappInfo = new File(webappDir, contentDir);
if(!webappInfo.exists()){
File dir = new File(webappDir, contentDir);
if(!dir.exists() && dir.mkdir()){
webappDir = dir;
}
}
webappDir = webappInfo;
}
this program create new folder in specified path like "C:/solution/totalsolution/war/content/abcd/xysa.mp4"
when i show any resource then i make relative path like this
http://localhost:8080/solution/content/abcd/xysa.mp4
till now every thing is ok.But when i extract war file and deploye it into server.
Then is way failed due to static location in system property.
So can any body tell be the right approch to save data into war folder and right back reading it on local as well as on server to.
1) Do not store it in the war folder but in a separate location (probably designed specifically for this).
2) When getting the file from the user, you need to generate new unique name for this file, and store somewhere (e.g. DB) the mapping from the original file name to the new file name. Then on the disk you store the file under the new name. This is to guarantee your file names are unique (think what happens if two users upload files with the same name).
Don't try to save data into your webapp; there is no guarantee that this directory will be writable or even unpacked from the jar. Instead, read a property that tells you where to store data saved at runtime.
I want to make a program that you can email to someone and they can run it.
Right now my code for making a file is like this:
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml);
f.createNewFile();
But what if someones username is not S0urceC0ded, or they put the project in a different place? How could I set the file path to the src folder plus the filename?
Leave the path off entirely, it will use the directory of the project.
Change
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml");
To
File f = new File("axmlfile.xml");
I generally use code like this for temporary file storage, this way it gets cleaned up when the application finishes. If required you can allow the user to save a version of the file or move it to a permanent location.
try{
//create a temporary file
File temp = File.createTempFile("axmlfile", ".xml");
System.out.println("Location: " + temp.getAbsolutePath());
}catch(IOException e){
e.printStackTrace();
}
I have created a file stored on internal storage from an activity. How can I delete this file from another activity?
I'm thinking I'll have to get the file's directory (which I'm not sure how to) and delete it. I tried using
context.deleteFile();
but it won't work because I'm trying to call it from a non-static method.
Here is your answer :
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
You can try getting the instance pointing to the file and deleting it like in
this answer
or this one
In my app, I used this code:
File DirectoryPath = cw.getDir("custom", Context.MODE_PRIVATE);
While creating a directory, and it returns:
/data/data/com.custom/app_custom**
So my question is why this app_ appears along with directory name. I know its default, but what actually it means?
And secondly, how can I create a sub-directory inside my directory i.e. app_custom in this case. if anyone knows please help me to understand this concept of getDir.
As far as I think, automatic "app_" added to user created data folders avoid any conflicts with system predefined application folders (folders inside application data folder i.e. cache, contents, databases etc. which are automatically created).
One method to create a sub folder inside those "app_..." folders, get absolute path of "app_..." folder, append required folder name to that and create using mkdirs()
e.g.
File dir = new File(newFolderPath);
dir.mkdirs()
Note: sub folders do not get "app_..." prefix
You can create a new Directory using the path that you are getting from getDir(),
File file = getDir("custom", MODE_PRIVATE);
String path = file.getAbsolutePath();
File create_dir = new File(path+"/dir_name");
if(!create_dir.exists()){
create_dir.mkdir();
}