J'ai besoin d'une application simple pour créer un fichier texte (.txt) dans le téléphone (racine) avec différentes chaînes de diverses éditions. Un bouton confirme les données d'entrée.Android - enregistrer un fichier texte à partir d'edittext - fichier introuvable
Un exemple de sortie dans le fichier doit être:
FileName.txt
John ; Smith ; 10/05/1970 ;
Le problème est le suivant: il n'y a pas d'erreur dans l'émulateur, mais quand j'ouvre le fichier apk dans le vrai téléphone et l'application confirme que le fichier a été enregistré dans le répertoire /data/user/0/com.example.utente.questionario/files lorsque je recherche ce fichier FileName.txt dans le téléphone (j'utilise Android 7) je didn ' t trouver n'importe quel fichier et je trouve seulement le dossier vide/data /. Quel est le problème?
Voici le code:
MainActivity.java
package com.example.utente.questionario;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private String getOrderText(String aa, String bb, String cc){
StringBuilder sb = new StringBuilder();
sb.append(aa+ ";");
sb.append(" " + bb+ ";");
sb.append(" " + cc);
return sb.toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
final EditText editText2 = (EditText) findViewById(R.id.editText2);
final EditText editText3 = (EditText) findViewById(R.id.editText3);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editText1.getText().toString();
String surname = editText2.getText().toString();
String birthDate = editText3.getText().toString();
try {
FileOutputStream fileOut=openFileOutput("FileName.txt", MODE_APPEND);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileOut);
outputWriter.write(getOrderText(name, surname, birthDate));
outputWriter.write("\n");
outputWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this,
String.valueOf("The file was saved in " + MainActivity.this.getFilesDir().getAbsolutePath()), Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.utente.questionario.MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Nome"
android:layout_marginTop="74dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Cognome"
android:layout_below="@+id/editText1"
android:layout_alignStart="@+id/editText1"
android:layout_marginTop="42dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data di nascita"
android:textSize="18dp"
android:layout_marginBottom="67dp"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genera file"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date"
android:hint="gg/mm/aaaa"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/editText2" />
</RelativeLayout>
Confirmez que vous avez donné l'autorisation WRITE_EXTERNAL_STORAGE pour cette application et que vous l'avez autorisée. – 9spl
WRITE_EXTERNAL_STORAGE n'est pas nécessaire parce que questioneer le stocke dans le stockage interne .. – Opiatefuchs
Je suppose que vous accédez au mauvais chemin. Vous devez rechercher un chemin comme 'data/data/yourapplicationpackage.com/files' car vous avez enregistré le fichier sur le stockage interne .... – Opiatefuchs