0

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>
Miche85
  • 5
  • 6
  • Confirm you have give WRITE_EXTERNAL_STORAGE permission for this app.and permitted it. – 9spl Apr 06 '17 at 07:18
  • WRITE_EXTERNAL_STORAGE is not needed because questioneer stores it inside the internal storage.. – Opiatefuchs Apr 06 '17 at 07:32
  • I guess you are accessing the wrong path. You need to search for a path like `data/data/yourapplicationpackage.com/files` because you saved the file on internal storage.... – Opiatefuchs Apr 06 '17 at 07:36

1 Answers1

1

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.

Nadimuddin
  • 230
  • 3
  • 17
  • on `data` directory, there is the internal storage, so the app has access to it. If you save a file like questioneer did (only with file name without directory) it will be saved to that storage. For example `data/data/apppackage.com/files/` So "directory is not accessible.." is not correct....only for searching with a file explorer on a not rooted device.. – Opiatefuchs Apr 06 '17 at 07:29
  • not accessible means normal user can't see that directory from explorer, am I right? – Nadimuddin Apr 06 '17 at 07:34
  • if you mean this, than you are right and I misunderstood your answer. it´s right if the phone is not rooted (or app is restricted from root access). – Opiatefuchs Apr 06 '17 at 07:35
  • thanks for correcting me @Opiatefuchs I have edited my answer. – Nadimuddin Apr 06 '17 at 07:40
  • How I can give a root to my phone? – Miche85 Apr 11 '17 at 20:45
  • I found by myself the answer: http://www.digitaltrends.com/mobile/how-to-root-android/ – Miche85 Apr 12 '17 at 06:41
  • Another question: the saving of this text file is possibile only in a rooted phone? Is possible the saving of the text file in an unrooted device (no matter whether in a folder or in another)? – Miche85 Apr 12 '17 at 07:37
  • You can save file in any directory other than "root directory", for e.g. folders like DCIM, PICTURES etc. This things u can do in unrooted device also. http://stackoverflow.com/questions/41479513/phonegap-how-to-save-txt-file-in-root-directory-of-android-mobile/41691057#41691057 this link will help u to get these folders from phone storage. – Nadimuddin Apr 12 '17 at 08:14
  • I read your answer to that question, if I want to save the file in the DCIM folder I must write String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath(); but How I can write the file with FileOutputStream and oututWriter? – Miche85 Apr 12 '17 at 09:47
  • @Miche85 in that answer I have mention how to write file with FileWrite. If u want with FileOutputStream then u can get ur answer here https://www.mkyong.com/java/how-to-write-to-file-in-java-fileoutputstream-example/ if my answer is useful don't forget to give thump up. – Nadimuddin Apr 13 '17 at 11:03