0

我在从外部存储创建目录时遇到问题,这是我正在使用的方法。

public String createDir(String npath,String ndir,String file)
{
    String[] paths = new String[] { npath, npath + ndir };

    for (String path : paths) {
        File dir = new File(path);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
                return "ERROR: Creation of directory " + path + " on sdcard failed";
            } else {
                Log.v(TAG, "Created directory " + path + " on sdcard");
            }
        }

    }
    if (!(new File(npath + ndir + file)).exists()) {
        try {
            AssetManager assetManager = getAssets();
            InputStream in = assetManager.open(ndir + file);
            OutputStream out = new FileOutputStream(npath + ndir + file);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

            Log.v(TAG, "Copied " + file);
            return "Copied " + file;
        } catch (IOException e) {
            Log.e(TAG, "Was unable to copy " + file + e.toString());
            return "Unable to copy " + file + e.toString();
        }
    }
    else
        return "Error!";
}

但我总是得到“错误:在 sdcard 上创建目录”+路径+“失败”。更不用说这已经安装在安卓设备中了。我试图寻找我的应用程序的目录,但找不到它,据说应该在安装我的 APK 后自动创建它的文件夹。谁能解决这个问题?

4

0 回答 0