Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android 4.4.4在三星S4上保存到SD_Java_Android_Sd Card - Fatal编程技术网

Java Android 4.4.4在三星S4上保存到SD

Java Android 4.4.4在三星S4上保存到SD,java,android,sd-card,Java,Android,Sd Card,我正在尝试将文件保存到我的SD,但我无法将其保存到,我甚至尝试将应用程序移动到SD以查看是否可以。我真的不在乎它在那里结束,但这不是;t工作: String state = Environment.getExternalStorageState(); File filesDir; if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media

我正在尝试将文件保存到我的SD,但我无法将其保存到,我甚至尝试将应用程序移动到SD以查看是否可以。我真的不在乎它在那里结束,但这不是;t工作:

    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;
}
public HashSet getExternalMounts(){
final HashSet out=新HashSet();
String reg=“(?i)。*vold.*(vfat | ntfs | exfat | fat32 | ext3 | ext4)。*rw.*;
字符串s=“”;
试一试{
最终进程=新建ProcessBuilder()。命令(“装载”)
.redirectErrorStream(true).start();
process.waitFor();
最终的InputStream=process.getInputStream();
最终字节[]缓冲区=新字节[1024];
while(is.read(buffer)!=-1){
s=s+新字符串(缓冲区);
}
is.close();
}捕获(最终异常e){
e、 printStackTrace();
}
//解析输出
最终字符串[]行=s.split(“\n”);
用于(字符串行:行){
如果(!line.toLowerCase(Locale.US).contains(“asec”)){
如果(行匹配(reg)){
String[]parts=line.split(“”);
用于(字符串部分:部分){
if(部分开头以(“/”)开头)
如果(!part.toLowerCase(Locale.US).contains(“vold”))
加上(部分);
}
}
}
}
返回;
}

Android 4.4实施了大量SD卡限制,在此期间,由于SD卡写入限制问题,有(现在仍然有)很多应用程序出现了中断。对我来说,你的代码本身似乎很好,但我相信是安卓4.4的SD卡限制确保了它无法工作。如何解决这个问题(没有根访问权限)是我无法理解的。

Android 4.4实施了许多SD卡限制,在这段时间内,由于SD卡写入限制的问题,有(现在仍然有)很多应用程序被破坏。对我来说,你的代码本身似乎很好,但我相信是安卓4.4的SD卡限制确保了它无法工作。如何解决这个问题(没有根访问权限)是我无法理解的。

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

  • getExternalFilesDirs()
  • getExternalCacheDirs()
  • getExternalMediaDirs()
    (这个是在Android 5.0 IIRC中添加的)
请注意这些方法名称的复数形式。如果返回2+个条目,则第二个和后续条目应位于可移动媒体上,位于应用程序唯一的目录中。您可以在没有任何
元素的情况下读取和写入这些目录


您还可以考虑存储访问框架(<代码> ActhOnOpenOpDeX < /COD>和KIN),以允许用户选择文件放在何处,无论是在外部存储还是可移动存储或谷歌驱动器或其他什么。

< P> Android 4.4 +允许您写入可移动介质,但仅限于通过以下方法获得的选定点:

  • getExternalFilesDirs()
  • getExternalCacheDirs()
  • getExternalMediaDirs()
    (这个是在Android 5.0 IIRC中添加的)
请注意这些方法名称的复数形式。如果返回2+个条目,则第二个和后续条目应位于可移动媒体上,位于应用程序唯一的目录中。您可以在没有任何
元素的情况下读取和写入这些目录


您还可以考虑存储访问框架(<代码> ActhOnOpenOpDeX < /COD>和KIN),以允许用户选择文件放在何处,无论是在外部存储或可移动存储或谷歌驱动器上,或是什么。

“但这不是工作”——请解释这意味着什么。请记住,您的第二个代码片段,Android 4.4在大约18个月前首次发布,目前,安卓4.4+为谷歌Play生态系统的一半设备提供了动力。@commonware我的意思是,除了在设备上,我永远无法让它在任何地方写东西storage@CommonsWare阅读你的博客是否意味着4.4+将不再允许我们写SD?4.4+上有一些特定的方法,比如
getExternalFilesDirs()
(注意复数),它可能返回可移动存储器上的位置。我们还强烈建议您使用存储访问框架,并让用户指出用户希望在何处存储数据流,其中可能包括可移动媒体、Dropbox等网络服务。@commonware getExternalFilesDirs()工作得非常出色,我终于能够写入SD卡了!“但这不起作用”——请解释一下这是什么意思。请记住,您的第二个代码片段,Android 4.4在大约18个月前首次发布,目前,安卓4.4+为谷歌Play生态系统的一半设备提供了动力。@commonware我的意思是,除了在设备上,我永远无法让它在任何地方写东西storage@CommonsWare阅读你的博客是否意味着4.4+将不再允许我们写SD?4.4+上有一些特定的方法,比如
getExternalFilesDirs()
(注意复数),它可能返回可移动存储器上的位置。我们还强烈建议您使用存储访问框架,并让用户指出用户希望在何处存储数据流,其中可能包括可移动媒体、Dropbox等网络服务。@commonware getExternalFilesDirs()工作得非常出色,我终于能够写入SD卡了!