如何在Android的内部存储中写入文件到文件夹?

7

我编写了一个方法,它创建一个文件并将数据写入文件并存储在内部存储中。当我获取文件的绝对路径或路径时[我添加了日志消息以尝试在文件上执行操作],它显示该文件正在根目录下创建,并且在/data/data/mypackagename/files/filename.txt下。尽管如此,我可以在DDMS上找到这些文件夹,我可以找到由我编写的方法创建的文件。但是我无法打开该文件,因为我没有权限。

当我查看我的Android设备时,找不到这些目录。我在stackoverflow上搜索,有些人回答说内部存储中的/data/data文件夹是隐藏的,要访问它们,我必须root设备,但我不想这样做。

下一步方法:android设备中有一个名为MyFiles的文件夹[我使用运行Android 4.4的Galaxy Tab 4进行测试]。在这个文件夹下有一个Device Storage文件夹,其中包含各种文件夹,例如Documents、Pictures、Music、Ringtones、Android等等。因此,像相机、电子表格应用程序这样的应用程序能够将图片保存到pictures文件夹或在documents文件夹中保存txt文件。同样,我如何将我在函数中创建的文件写入Documents文件夹或可在设备上访问的任何其他文件夹。请帮助我如何做到这一点,非常感谢。

以下是我编写的代码:

public void addLog(String power_level){
// creates a logFile in the root directory of the internal storage of the application.
// If the file does not exists, then it is created.
Log.d("AppendPower", "In addLog method");
//File logFile = new File(((Context)this).getFilesDir(), "logFile.txt");
File logFile = new File(getFilesDir(), "logFile.txt");
Log.d("FilesDir Path", getFilesDir().getAbsolutePath());
Log.d("FilesDir Name", getFilesDir().getName());
Log.d("Path on Android", logFile.getPath());
Log.d("Absolute Path on Android", logFile.getAbsolutePath());
Log.d("Parent", logFile.getParent());
if(!logFile.exists()){
    try{
        logFile.createNewFile();
    }catch(IOException io){
        io.printStackTrace();
    }
}
try{
    BufferedWriter writer = new BufferedWriter(new FileWriter(logFile, true));
    writer.write("Battery level reading");
    writer.append(power_level);
    Log.d("Power_Level in try", power_level);
    writer.newLine();
    writer.close();
}catch(IOException e){
    e.printStackTrace();
}

}

3个回答

9
您已经意识到,在Android中写入root目录是不可能的,除非您对设备进行了Root。这就是为什么即使有些应用在Play商店安装之前都会要求获取Root权限。但是,Root会使您的保修失效,因此如果您没有严重要求,我不建议您这样做。
除了Root目录之外,您可以访问任何在Android文件管理器中可见的文件夹。
以下是如何使用一些数据将文本文件写入SD卡的方法 - 参考来源:https://dev59.com/XGsz5IYBdhLWcg3wCDh2#8152217 使用这些代码,您可以在SDCard中写入文本文件,并需要在Android Manifest中设置权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这是代码:

public void generateNoteOnSD(String sFileName, String sBody){
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();
         importError = e.getMessage();
         iError();
    }
   }  

.


1

1) 如果您的目的是调试,您可以直接写入到/sdcard/。这总是有效的。

2) 同样,如果您的目的是调试,您可以尝试在您的应用目录上设置读取权限。一段时间以前,在某些Android设备上它对我有效(但至少有一个设备上无效)。


所有设备都没有SD卡,对吧。现在每个人的设备上都有内部存储器。而且这并不是为了调试。我想将文件写入内部存储器! - ShellZero
1
/sdcard/ 可以工作。它通常指的是“内置”的SD卡,其实并不是真正的SD卡,但为了兼容性而存在。如果你的Java代码写入 /sdcard/,而 adb pull 从同样的 /sdcard/ 读取,无论是否是真正的SD卡都没有问题。 - 18446744073709551615
1
+1。是的,我明白了。非常感谢。我不知道内部存储其实就是内置SD卡。 - ShellZero

0
请在您的 AndroidManifest.xml 文件中添加此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

然后使用这个最短的方法:
try
{
   FileOutputStream fos = 
      openFileOutput("myfile.txt", getApplicationContext().MODE_PRIVATE);
   fos.write("my text".getBytes());
   fos.close();
}
catch (Exception exception)
{
   // Do something, not just logging
}

它将保存在"/data/data/my.package.name/files/"路径中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接