2016-07-27 147 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

相关问题