为什么我不能在Android中成功写入外部SD卡?

为什么我不能在Android中成功写入外部SD卡?,android,Android,我正在尝试将我的应用程序的日志文件写入我的外部SD卡。为了弄清楚如何做到这一点,我一直在努力学习这个例子 在这一页的下方有一个大约3/4的部分,上面写着: 如果预定义的子目录名称都不适合您的文件,则可以调用getExternalFilesDir并传递null。这将返回应用程序在外部存储上的私有目录的根目录 我的设备没有任何预定义的目录,我宁愿将数据保存到外部SD卡。使用以下命令: $ ADB shell shell@android:/ $ cd /mnt shell@android:/ $ ls

我正在尝试将我的应用程序的日志文件写入我的外部SD卡。为了弄清楚如何做到这一点,我一直在努力学习这个例子

在这一页的下方有一个大约3/4的部分,上面写着:

如果预定义的子目录名称都不适合您的文件,则可以调用getExternalFilesDir并传递null。这将返回应用程序在外部存储上的私有目录的根目录

我的设备没有任何预定义的目录,我宁愿将数据保存到外部SD卡。使用以下命令:

$ ADB shell
shell@android:/ $ cd /mnt
shell@android:/ $ ls -F
d ext_sdcard
你可以看到我的外部SD卡。因此,我在外部SD卡上创建了一个目录:

shell@android:/ $ cd ext_sdcard
shell@android:/ $ mkdir MyAwesomeApplication
shell@android:/ $ ls -F
d MyAwesomeApplication
现在是我[某种程度上]使用Android开发者网站上的代码的时候了:

public File getDocumentStorageDir(String filename){
    File file = new File("/mnt/ext_sdcard/MyAwesomeApplication", filename);
    if(!file.getParentFile().mkdirs()){
        Log.i(MainActivity.class.getName(), "Directory Not Created!");
    }
    return file;
}
当我运行我的应用程序时,它可以正常工作;我立即发现这个错误:

E/myPackage.MainActivity: Directory Not Created!
但它还是创建了日志文件,并成功地将数据写入该文件

我的问题是,它总是用新数据覆盖日志文件,它不会追加,即使我正在使用:

public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

if(isExternalStorageWritable()){
    File logfile = getDocumenetStorageDir("mylogfile.log");
    PrintWriter logFileWriter = null;
    try {
        logFileWriter = new PrintWriter(logfile);
        logFileWriter.append("stuff ...");
    } catch(FileNotFoundException fnfe){
        Log.i(MainActivity.class.getName(), fnfe.getMessage());
    } finally {
        logFileWriter.close();
    }
}
现在,当我使用此命令时:

shell@android:/ $ cd MyAwesomeApplication
shell@android:/ $ ls -F
- mylogfile.log
shell@android:/ $ cat mylogfile.log
// only the very latest piece of data is here
我看到我的日志文件应该在哪里。但是当我使用这个命令时:

shell@android:/ $ cd MyAwesomeApplication
shell@android:/ $ ls -F
- mylogfile.log
shell@android:/ $ cat mylogfile.log
// only the very latest piece of data is here
我在这里做错了什么


谢谢

你的问题分为两个部分,我将分别回答

文件的位置 即使只是调试日志记录,也不应该像现在这样硬编码文件位置。位置文件File=new File/mnt/ext_sdcard/MyAwesomeApplication,filename;可能适用于此设备,但这取决于Android版本和制造商。您必须使用适当的getter方法来获取正确的目录。在上,您可以找到完整的解释,但根据我对您的用例的理解,您希望使用以下代码:

public static File getLogFile(Context context) {
   File folder = getExternalFilesDir("logs");
   if(!folder.exists()) {
       folder.mkdirs();
   }
   return new File(folder, "mylogfile.log");
}
附加到现有文件。 您没有展示如何创建PrintWriter,但我一直使用FileWriter类,没有任何附加问题

在这个问题上你可以看到。这里有一些代码

FileWriter f;
try {
    f = new FileWriter(getLogFile(context), true); // true is for append
    f.write(" ..logs...");
    f.flush();
} catch(Exception e) {
     .. handle exceptions
} finally {
    if(f != null) {
        f.close();
    } catch(Exception ee){
     ... exceptions might happen here too
    }
}

抱歉,我忘记添加创建PrintWriter的行。我现在已经添加了它,它在try块中。至于硬编码位置,此代码将只在一台设备上使用。即使此代码将在该设备上使用,错误的做法也是一种错误的做法,始终使用正确的代码也无妨。试着用FileWriter来写日志,我从来没有遇到过问题。此外,我不知道如何在没有硬编码的情况下以编程方式获取ext_sdcard。我已经在回答中说明了如何获取外部存储,这应该是很好的。如果您绝对必须拥有实际的SD卡本身,那么就没有那么简单了,请检查以下问题:当我使用getExternalFilesDirlogs时,它返回对/mnt/sdcard/…的引用。。。而不是/mnt/ext_sdcard/。。。