Search code examples
androidfile-permissionsandroid-permissions

Create a file in Android with certain permission


I'm developing an Android application I have to implement a function that create a folder with different files.

I wish the files in the folders were hidden, and this point is not a problem, but I want assign certain permission to files, for example I need that files are not readable / writable by user but only from application.

Also I wish the files in the folder were deleted if the application was uninstalled.

This is the code that i use to create hidden folder:

File JSONStorage = new File(Environment.getExternalStorageDirectory(), ".BMA");
if (!JSONStorage.exists()) {
    if (!JSONStorage.mkdirs()) {
        Log.wtf("log: ", "Failed to create directory");
    }
}

Solution

  • From official Android documentation:

    String FILENAME = "hello_file";
    String string = "hello world!";
    
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
    

    This will create file on device internal memory. This file can be read only by application. (Context.MODE_PRIVATE)