Reputation: 5
I need a simple app making a text file (.txt) in the phone (root) with inside various strings from various edittexts. A button confirms the input data.
An example of output in the file must be:
FileName.txt
John ; Smith ; 10/05/1970 ;
The problem is this: there is no error in the emulator, but when I open the apk file in the real phone and the app confirms that the file was saved in the directory /data/user/0/com.example.utente.questionario/files when I search this file FileName.txt in the phone (I use Android 7) I didn't find any file and I find only the empty folder /data/. What is the problem?
This is the 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>
Upvotes: 0
Views: 717
Reputation: 239
It seems that your phone is not rooted. File is properly saving in that directory but you can't find it because normal user can't see /data/...
directory.
Upvotes: 1