Android 4.4.4在Samsung S4上保存到SD

时间:2015-05-04 17:44:49

标签: java android sd-card

我试图将文件保存到我的SD但我无法理解,我甚至尝试将应用程序移动到SD以查看是否可以。我真的不在乎它最终会在哪里,但这不起作用:

    String state = Environment.getExternalStorageState();
    File filesDir;

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        filesDir = getExternalFilesDir(null);
        Log.i(Utils.TAG, "We can read and write the media: " + filesDir.getAbsolutePath()); // This is the local on the phone
    } else {
        // Load another directory, probably local memory
        filesDir = getFilesDir();
        Log.i(Utils.TAG, "Load another directory, probably local memory: " + filesDir.getAbsolutePath());
    }

    try {
       // Creates a trace file in the primary external storage space of the 
       // current application.
       // If the file does not exists, it is created.
       //File traceFile = new File(((Context)this).getExternalFilesDir(null), "TraceFile.txt"); //This one saves to the internal file folder
        File traceFile = new File(filesDir, "TraceFile.txt");

        Log.i(Utils.TAG, traceFile.getAbsolutePath());

       if (!traceFile.exists())
          traceFile.createNewFile();
                                // Adds a line to the trace file
       BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, true /*append*/));
       writer.write("This is a test trace file.");
       writer.close();
                               // Refresh the data so it can seen when the device is plugged in a
                               // computer. You may have to unplug and replug the device to see the 
                               // latest changes. This is not necessary if the user should not modify
                               // the files.
        MediaScannerConnection.scanFile((Context)(this),
                                         new String[] { traceFile.toString() },
                                         null,
                                         null);

    } catch (IOException e) {
        Log.i(Utils.TAG, "Unable to write to the TraceFile.txt file.");
    }

然而,这给了我SD文件,但我无法写信给它:

public HashSet<String> getExternalMounts() {
    final HashSet<String> out = new HashSet<String>();
    String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
    String s = "";
    try {
        final Process process = new ProcessBuilder().command("mount")
                .redirectErrorStream(true).start();
        process.waitFor();
        final InputStream is = process.getInputStream();
        final byte[] buffer = new byte[1024];
        while (is.read(buffer) != -1) {
            s = s + new String(buffer);
        }
        is.close();
    } catch (final Exception e) {
        e.printStackTrace();
    }

    // parse output
    final String[] lines = s.split("\n");
    for (String line : lines) {
        if (!line.toLowerCase(Locale.US).contains("asec")) {
            if (line.matches(reg)) {
                String[] parts = line.split(" ");
                for (String part : parts) {
                    if (part.startsWith("/"))
                        if (!part.toLowerCase(Locale.US).contains("vold"))
                            out.add(part);
                }
            }
        }
    }
    return out;
}

2 个答案:

答案 0 :(得分:2)

Android 4.4实现了许多SD卡限制,并且在此期间有很多应用程序破坏了,因为SD卡的写入限制存在问题。您的代码本身对我来说似乎很好,但我相信它是Android 4.4的SD卡限制,可确保它无法正常工作。如何解决这个问题(没有root访问权限)超出了我的范围。

答案 1 :(得分:2)

Android 4.4+允许您写入可移动媒体,但仅限于通过以下方法获得的选定位置:

  • getExternalFilesDirs()
  • getExternalCacheDirs()
  • getExternalMediaDirs()(这个是在Android 5.0 IIRC中添加的)

请注意这些方法名称的复数形式。如果他们返回2个以上的条目,则第二个和后续条目应位于可移动媒体上,位于您的应用程序唯一的目录中。您可以在没有任何<uses-permission>元素的情况下读取和写入这些目录。

您还可以考虑存储访问框架(ACTION_OPEN_DOCUMENT和亲属),以允许用户选择放置文件的位置,无论是在外部存储或可移动存储或Google云端硬盘等等。