简体   繁体   English

Android 4.4.4在Samsung S4上保存到SD

[英]Android 4.4.4 Save to SD on Samsung S4

I'm attempting to save files to my SD but i cannot get it to, I even tried moving the app to the SD to see if I can. 我正在尝试将文件保存到我的SD上,但我无法将其保存到,我什至尝试将应用程序移动到SD上以查看是否可以。 I don't really care where it ends up on there but this isn;t working: 我并不在乎它到底在哪里,但这是行不通的:

    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.");
    }

However, this gave me the SD file but I couldn't write to it: 但是,这给了我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 implemented numerous SD card limits, and there were (and still are) a lot of apps which broke during this period, because of issues with write limitations to the SD card. Android 4.4实施了许多SD卡限制,由于SD卡的写入限制问题,在此期间(至今)有很多应用中断了。 Your code itself seems fine to me, but my believe is that it's Android 4.4's SD Card limits which ensure that it doesn't work. 您的代码本身对我来说似乎还不错,但我相信这是Android 4.4的SD卡限制,可确保其无效。 How to fix that (without root access) is beyond me. 如何解决该问题(无root访问权限)超出了我的范围。

Android 4.4+ allows you to write to removable media, but only in select spots obtained by methods like: Android 4.4+允许您写入可移动媒体,但只能写入通过以下方法获得的特定位置:

  • getExternalFilesDirs()
  • getExternalCacheDirs()
  • getExternalMediaDirs() (this one was added in Android 5.0 IIRC) getExternalMediaDirs() (此在Android 5.0 IIRC中添加)

Note the plural on those method names. 请注意这些方法名称上的复数形式。 If they return 2+ entries, the second and subsequent ones should be on removable media, in a directory that is unique for your app. 如果它们返回2个以上的条目,则第二个及后续条目应位于可移动媒体上,位于您应用程序唯一的目录中。 You can read and write to those directories without any <uses-permission> element. 您可以在没有任何<uses-permission>元素的情况下读写这些目录。

You might also consider the Storage Access Framework ( ACTION_OPEN_DOCUMENT and kin), to allow the user to choose where to place the file, whether that be on external storage or removable storage or Google Drive or whatever. 您还可以考虑使用Storage Access Framework( ACTION_OPEN_DOCUMENT和kin),以允许用户选择将文件放置在何处,无论该文件位于外部存储或可移动存储中,还是位于Google Drive中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM