java - Android 4.4.4 在三星 S4 上保存到 SD

标签 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;
}

最佳答案

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

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

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

您还可以考虑存储访问框架(ACTION_OPEN_DOCUMENT 和 kin),以允许用户选择放置文件的位置,无论是在外部存储、可移动存储还是 Google Drive 或其他任何地方。

关于java - Android 4.4.4 在三星 S4 上保存到 SD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30036481/

相关文章:

java - Eclipse Luna如何添加执行环境1.8?

java - Android:如何将我的 Android 模拟器连接到互联网?

android - 无法使用 firebase-config :16. 0.0 和 firebase-core :16. 0.1 构建

java - 如何使用 Hashmap 在 firebase firestore android 中添加 Map<String, Object> 元素?商店数据库

http - mbed:建立网络连接后访问 SD 卡时出现 HardFault 错误

java - Hadoop:java.net.ConnectException:连接被拒绝

java - 如何让机器保持清醒?

android - 谷歌开发控制台测试,无需将 apk 发送到生产环境

android - 如何在 Android 中阅读 PDF?

android挂载卸载sd卡