Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating directory in application package on internal storage

Tags:

android

Is it possible for android application to create several directories in internal storage for storing there different kinds of files? I need this capability, because I'll need to delete a kind of files at definite time moment in my application.

When I try to use standard Context method openFileOutput() and send to it filename with "/" symbol I get IllegalArgumentException.

Tell me please what classes and methods could allow me such functionality?

like image 809
teoREtik Avatar asked Aug 18 '11 10:08

teoREtik


People also ask

How do you allow other apps to access files stored in this directory within internal storage?

To allow other apps to access files stored in this directory within internal storage, use a FileProvider with the FLAG_GRANT_READ_URI_PERMISSION attribute.


1 Answers

Use Context.getDir(String name, int mode) method to create or access directories in internal storage. Quote from docs:

Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.

UPD Example:

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
like image 94
Konstantin Burov Avatar answered Oct 07 '22 11:10

Konstantin Burov