Una app può salvare e leggere file nella memoria interna come file privati a cui solo quella app può accedere ad eccezione di altre app con diritti di root.
Il percorso assoluto di questa cartella è data\data\[your app package]\files
La classe Context fornisce alcuni metodi che facilitano alcune operazioni come:
openFileInput(String name)
Apre un file privato associato con questo Context’s application package per la letturaopenFileOutput(String name, int mode)
Apre un file privato associato con questo Context’s application package per la scritturagetFilesDir()
Ottiene il percorso assoluto alla cartella dove i tuoi file interni sono salvatigetDir()
crea una (o apre una esistente) cartella nella memoria internadeleteFile()
Elimina un file salvato nella memoria internafileList()
Restituisce una matrice di file salvati dalla tua applicazione
e non servono speciali permessi per utilizzare questi metodi.
Per esempio il seguente codice tratto da Using the Internal Storage crea un file contenente la stringa “hello world!”
1 2 3 4 5 6 7 8 9 10 11 12 |
try { String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); } catch (IOException e) { Log.e("ERROR", e.toString()); } |
dove alla linea 5, al posto di MODE_PRIVATE si può usare MODE_APPEND, altre costanti come MODE_WORLD_READABLE e MODE_WORLD_WRITEABLE sono deprecati.
Il seguente codice legge lo stesso file e mostra un messaggio con il testo “hello world!”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
try { String FILENAME = "hello_file"; byte[] bytes = new byte[1024]; FileInputStream fis = openFileInput(FILENAME); fis.read(bytes); fis.close(); String string = new String(bytes); Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show(); } catch (IOException e) { Log.e("ERROR", e.toString()); } |
Ma questi esempi hanno un limite: essi non possono essere usati in sotto cartelle, cioè non è possibile creare una struttura ad albero di directory e file.
Se necessiti di creare file internamente a una directory devi usare le tecniche standard di java, per esempio il codice seguente crea un file interno alla cartella “sub”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
try { String FILENAME = "hello_file"; String FOLDERNAME = "sub"; String string = "hello world!"; Context context = getApplicationContext(); String folder = context.getFilesDir().getAbsolutePath() + File.separator + FOLDERNAME; File subFolder = new File(folder); if (!subFolder.exists()) { subFolder.mkdirs(); } FileOutputStream outputStream = new FileOutputStream(new File(subFolder, FILENAME)); outputStream.write(string.getBytes()); outputStream.close(); } catch (FileNotFoundException e) { Log.e("ERROR", e.toString()); } catch (IOException e) { Log.e("ERROR", e.toString()); } |
e il seguente codice legge lo stesso file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
try { String FILENAME = "hello_file"; String FOLDERNAME = "sub"; byte[] bytes = new byte[1024]; Context context = getApplicationContext(); String folder = context.getFilesDir().getAbsolutePath() + File.separator + FOLDERNAME; File subFolder = new File(folder); FileInputStream outputStream = new FileInputStream(new File(subFolder, FILENAME)); outputStream.read(bytes); outputStream.close(); String string = new String(bytes); Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { Log.e("ERROR", e.toString()); } catch (IOException e) { Log.e(TAG, e.toString()); } |
Hi, I have a question that is my homework,
Where are the files saved when the user does not specify the route?
data/data/[your package]/files
I don’t understand why I keep getting the same message: “error: cannot find symbol method getApplicationContext()” no matter what examples I choose. What do I need to eliminate it? Thank you for your answer!