无法写入Android SD卡中的应用程序特定目录

时间:2014-05-12 09:52:48

标签: android android-sdcard

我从KitKat了解到,应用程序只能写入其特定目录。

但奇怪的是我也无法写入我的特定应用程序目录。

获取SD卡目录的代码

Process process = new ProcessBuilder().command("mount").start();
    process.waitFor();

    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        // Output the line of output from the mount command
        logger.debug("   {}", line);

        if (line.startsWith("/dev/block/vold/")) {
            String[] tokens = line.split(" ");
            if (tokens.length >= 3 && (tokens[2].equals("vfat") || tokens[2].equals("exfat"))) {
                String path = tokens[1];
                File file = new File(path);
                if (file.exists() && file.isDirectory()) {
                    logger.debug("Detected SD card at {}", file.getPath());

                    if (!file.canWrite()) {
                        logger.warn("The SD card path {} is reporting that it is not writable", file.getPath());
                    }
                    // path = basecontext.getExternalFilesDir(null).getPath();

                    return path;
                }
            }
        }
    }

获取文件的代码

以下是我构建文件路径的方法: sdCardDirectory是我得到的目录:/storage/extSdCard/ 目录和子目录是我的应用程序sepcifc子目录,但显然在应用程序的应用程序特定目录中

sdCardDirectory + File.separator + "Android" + File.separator + "data" + File.separator
                    + <my app package> + File.separator + subdirectory
                    + File.separator + directory+ File.separator + document.getRepositoryId() + FILENAME_SEPARATOR
                + fetchObjectId(document);

检索的ID是简单的字母数字字符串,例如: aBc45ef_0

randomAccessFile = new RandomAccessFile(file, "rw");

我正在

java.io.FileNotFoundException: /storage/extSdCard/Android/data/myapp/cache/downloaded/OhCQL_RQl8IJcVlO5T1MX4-3SQg_mMDT5PWtf-IYmE0: open failed: EROFS (Read-only file system)

myapp是我的应用程序包名称。

更新这是我已打开的Android错误的链接https://code.google.com/p/android/issues/detail?id=69549&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

欢呼声, Saurav

1 个答案:

答案 0 :(得分:0)

  

但奇怪的是我也无法写入我的特定应用程序目录。

该代码不一定能为您提供任何可以使用的代码。请使用getExternalFilesDirs()(注意复数);返回列表中的第二个和后续条目将来自可移动存储(如果可用)。

您可能希望阅读my blog post on Android 4.4 and removable storage了解更多背景信息。