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
Related
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.
So my main idea is opening a uploaded file in res/raw directory, edit it, save and reopen it. But i also need a function to set default (overwrite csv file with the default one), so i need 2 files, the default one and the other one I edit and read.
Is it true that u can't write a file and store it in res/raw directory? If yes, then what's the best way to do it in this case? Should i save everything in internal/external storage and open later it from here?
try {
FileOutputStream fileOut = openFileOutput("C:\\Users\\textfile.csv", MODE_WORLD_WRITEABLE);
OutputStreamWriter outputWriter = new OutputStreamWriter(fileOut);
for (int b = 0; b < test.AddName("Karlos",1,1,vardadienas).size(); b++) {
outputWriter.write(test.AddName("Karlos",1,1,vardadienas).get(b));
outputWriter.write("\n");
}
outputWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
I use getResources() method and read the file (uploaded in resources folder), save it in a list array, edit the array and then i should save, but where? And also why this method doesnt create any file? I have set the path, but it doesn't create anything..
I also get this error:
at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:201)
at com.example.firstapp.firstapp.MainActivity.onCreate(MainActivity.java:86)
You can't write to res/raw as it is part of the apk file.
But you can read from res/raw ant then write a copy of the file in the app's local and private storage.
Then, whatever you planned to do with the file in res/raw you can do it in the local storage copy.
I have a problem checking if a file exists or not this is my simple code:
File myfile = new File ("SecretFile");
if(myfile.exists(){
TextView mytxt = (TextView)findViewById(R.id.textView);
mytxt.setText("Loaded successfully");
}
else{
try {
myfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
TextView mytxt = (TextView)findViewById(R.id.textView);
mytxt.setText("Not loaded,reboot the application");
}
I am doing a simple game and i need to check the first app run , because when the app runs it load all variables from files but on the first run there's no files so the app crashes.
I also have another question the File "SecretFile" where is located?
How can I create a file inside Android/data/com.mypackage.myapp/SecretFile ?
File myfile = new File ("SecretFile");
That statement has no meaning on Android.
I also have another question the File "SecretFile" where is located?
Nowhere. You are welcome to store your SecretFile on internal storage or external storage, but you must do so by creating a File object pointing to those locations (e.g., File myfile = new File(getFilesDir(), "SecretFile");).
How can I create a file inside Android/data/com.mypackage.myapp/SecretFile ?
You can't. The closest match is to use getExternalFilesDir(null) where I have getFilesDir() in my above code snippet, which would put your file in Android/data/com.mypackage.myapp/files/SecretFile on external storage. Also as Der Golem notes, you would need the WRITE_EXTERNAL_STORAGE permission on API levels 4 through 18.
In creating file in android, you need to add path where you will create file. If you cannot fill its requirement, file will not be created.
That's the reason why ur return data is always false.
This link can help you.
I'm trying to create a directory hierarchy and then copy a file from my assets into the newly created directory. I've used the code samples offered here on stackoverflow but I'm not getting anywhere. Essentially my code goes like this:
InputStream in = getAssets.open(mydb.sqlite);
File dir = new File("/data/data/my.package/databases/");
dir.mkdirs();
out = new FileOutputStream("/data/data/my.package/databases/mydb.sqlite");
while ((len = in.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close
This is all done within the main activity on first launch and of course should only be done once. Before the code runs, the only directory that exists is /data/. Yes, it is a database file but that shouldn't matter at this point since I'm just copying the file and haven't yet tried to access it with the database code.
The code doesn't throw any exceptions, and dir.mkdirs() returns true. Regardless, the directories are not created and the file is not copied. I have added the permissions line for WRITE_EXTERNAL_STORAGE to my manifest as has been suggested in several places, FWIW. I can step through the code and everything appears to be fine. But of course it's not.
What do I need to be looking for here? File permissions problems? Why does the code think everything is there (ie, when it gets to the copy code, it doesn't throw FileNotFoundException) but in reality there's nothing on the file system?
Edit: Here's what I've learned. getDir() creates the directory in /data/data/my.package/app_*. Not sure why it uses this prefix on the directory name. This won't work if you need to put something in a standard location such as databases/ or files/.
Use openFileOutput instead as /data/data is internal storage and you should only use the Android APIs to get a file handle there. See: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Edit:
/data/data/my.package is handled by openFileOutput/getDir so you would create your directory with getDir('databases',MODE_PRIVATE) and create your file there.
Edit2:
From getDir documentation:
You can use the returned File object to create and access files in this directory.
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();
}