2016-07-27 88 views
1

我是Android Studio的新手,並且經歷了大量教程。我有一個可用的加速計,並希望將數據保存到.csv文件。 我的代碼如下,因爲當我使用模擬器時,我可以將帶有正確數據的.csv文件導出到我的桌面並打開它。我在這兩個權限我AndroidManifest.xml訪問在Android設備上保存到內部存儲的數據

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

當我進入文件瀏覽器我的設備上使用應用程序,如天文或ES和去data/data/我甚至不看我的包存在,即使該應用程序適用於我的手機。有沒有什麼你需要做的,讓包顯示在文件瀏覽器?我想從手機中獲取.csv的數據。

我希望有人有一些在

String Entry = x.getText().toString() + ", " 
     + y.getText().toString() + ", " + 
     z.getText().toString() + "\n"; 
try{ 
    FileOutputStream outPut = openFileOutput(FILENAME, Context.MODE_APPEND); 
    outPut.write(Entry.getBytes()); 
    outPut.close(); 
}catch(Exception err){ 
    err.printStackTrace(); 
} 

編輯

String dir = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     String FILENAME = "keyboardTest.csv"; 
     File file = new File (dir, FILENAME) 
     try { 
       FileOutputStream out = new FileOutputStream(file, true); 
       out.write(Entry.getBytes()); 
       out.close(); 
      } catch (Exception err) { 
       err.printStackTrace(); 
      } 

現在我能夠訪問我的手機上的文件

+0

而問題是什麼? –

+0

@TodorKostov編輯問題 – csciBeginner

回答

3

您不能訪問這些文件(內部/數據/數據),直到您在模擬器上運行應用程序,或者您正在根設備上運行應用程序。

請查閱this瞭解更多詳情。

編輯

你可以做以下保存在可訪問位置的文件: -

/*get the path to internal storage*/ 
File path = Environment.getExternalStorageDirectory(); 
/*create the app folder and check if it exists or not*/ 
File curDir = new File(path.getAbsolutePath()+"/"+appName); 
if(!curDir.exists()){ 
     curDir.mkdirs(); 
} 
/*create the file*/ 
File f = new File(curDir+"/"+fileName); 
f.createNewFile(); 
/*code to edit the file*/ 
+0

@csciBeginner你可以檢查這個以及http://stackoverflow.com/questions/13006315/how-to-access-data-data-folder-in-android-device –

+0

我以爲你可以'訪問數據庫。我以爲csv或txt會很好,因爲它會放在文件夾中。在Android的視野中,他們建議按照我使用的方式存儲數據。 – csciBeginner

+0

如果您在運行Android M的設備上測試您的應用程序,那麼您將不得不在運行時詢問權限,還可能必須提供存儲文件的路徑。您將需要使用Environment.getExternalStorageDirectory()來獲取路徑並將文件保存在那裏 – shrijit