找到真正的SD卡路径并写入文件

时间:2015-02-11 21:08:40

标签: android android-4.4-kitkat sd-card

我的设备是htc一个双卡而且由于某种原因,Environment.getExternalStorageDirectory()是我对手机的记忆,它不是可移动的SD卡。 我试图用这个找到真正的SD卡路径:

   public static 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;
    }

我得到了 的/ mnt / media_rw / ext_sd

我尝试将文件写入/ mnt / media_rw / ext_sd / downloads 但文件似乎没有创建。

  File file = new File("/mnt/media_rw/ext_sd/downloads", "test.txt");
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            writer.write("sdfsdfsfd");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


02-11 23:12:35.236    9110-9110/www.jenyakirmiza.com.testsdcard W/System.err﹕ java.io.FileNotFoundException: /mnt/media_rw/ext_sd/downloads/test.txt: open failed: EACCES (Permission denied)

我从4.4开始听到有关限制的问题,所以现在我们无法将文件写入可移动的SD卡。但是他们说你可以把档案写到/sdcardpath/Android/data/your.package.name

PS。当然,我加上了write_external permisssion to manifest。

2 个答案:

答案 0 :(得分:1)

您可以找到Context.getExternalMediaDirs的所有外部存储空间,并可以使用Environment.isExternalStorageRemovable(File)检查它是否可移动。请注意,下载目录很可能只在主(模拟)外部存储中。

答案 1 :(得分:0)

尝试使用Environment.getExternalStorageDirectory()方法:

File file = new File(Environment.getExternalStorageDirectory() + "/Download/", "test.txt");

您可以使用exists()方法首先评估您的文件是否真的存在:

  File file = new File(Environment.getExternalStorageDirectory() + "/Download/", "test.txt");
    if(file.exists()){

      FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            writer.write("sdfsdfsfd");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

获得此许可非常重要:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在你的

里面
 AndroidManifest.xml