/ / Scrittura del file nella memoria interna - java, android

Scrittura di un file nella memoria interna - java, android

Voglio scrivere o inserire il mio file nella memoria interna di Android e sto facendo questo -

try {
File file = new File("/data/local/measurement.txt");

// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}

FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content.toString());
bw.close();

Log.d("hi", "WRITTEN");

} catch (IOException e) {
e.printStackTrace();
}

Ho già measurement.txt in / data / local path ma non vi è stato scritto nulla. Sto usando l'emulatore e ho anche dato le autorizzazioni come

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

risposte:

0 per risposta № 1
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(context.openFileOutput("myfile", MODE_PRIVATE)));
out.write(string);
out.close();

Questo inserirà il file /data/data/<pkg>/files, privato della tua app, a cui appartiene. Non hai bisogno di permessi per questo.