2017-04-06 44 views
0

我需要一個簡單的應用程序使得在手機(根),文本文件(.txt),與來自不同edittexts內的各種字符串。按鈕確認輸入數據。Android的 - 從節約的EditText文本文件 - 文件未找到

文件輸出的例子必須是:

FILENAME.TXT

John ; Smith ; 10/05/1970 ; 

問題是這樣的:有在模擬器中沒有錯誤,但是當我在打開apk文件真正的手機和應用程序確認該文件已保存在/data/user/0/com.example.utente.questionario/files目錄中,當我在手機中搜索此文件FileName.txt(我使用Android 7)時,找到任何文件,我只找到空文件夾/ data /。問題是什麼?

這是代碼:

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> 
+0

確認您已授予此應用程序的WRITE_EXTERNAL_STORAGE權限並允許它。 – 9spl

+0

WRITE_EXTERNAL_STORAGE不需要,因爲提問者將它存儲在內部存儲器中。 – Opiatefuchs

+0

我想你正在訪問錯誤的路徑。你需要搜索像'data/data/yourapplicationpackage.com/files'這樣的路徑,因爲你已經將文件保存在內部存儲器上了....數據'目錄下有 – Opiatefuchs

回答

1

看來,你的手機是不是植根。文件已正確保存在該目錄中,但無法找到它,因爲普通用戶看不到/data/...目錄。

+0

,這裏有內部存儲器,所以應用程序可以訪問它。如果你保存一個像questioneer這樣的文件(只有沒有目錄的文件名),它將被保存到那個存儲中。例如'data/data/apppackage.com/files /'So「目錄不可訪問..」是不正確的....僅用於在沒有根的設備上使用文件資源管理器搜索.. – Opiatefuchs

+0

不可訪問意味着普通用戶無法從資源管理器中看到該目錄,對嗎? – Nadimuddin

+0

如果你的意思是這樣,那你就是對的,我誤解了你的答案。如果手機沒有root權限(或者應用程序受限於root用戶訪問權限),這是正確的。 – Opiatefuchs