Android makedirs文件夹创建错误

时间:2016-03-08 12:06:25

标签: java android directory

最近我读到了android中的文件夹创建,并尝试在我的应用程序中实现它。如果dir不存在,函数应该生成,从webserver下载文件并解压缩。但是app无法在外部设备上创建文件夹。功能代码

public static void download(final int section){
    File folder = new File(Environment.getExternalStorageDirectory(), "/try2think"); //section"+section);
    if (!folder.exists()) {
        if(!folder.mkdirs()) {
            Log.i("Problem", "Problem creating folder");
        }
    }
    File folder1 = new File(Environment.getExternalStorageDirectory()+"/try2think", "section"+section);
    if (!folder1.exists()) {
        if(!folder1.mkdirs()) {
            Log.i("Problem", "Problem creating folder");
        }
    }
    String downloadURL = "http://jangofetthd.me/rebus/section" + section+".zip";
    final String directory = Environment.getExternalStorageDirectory()+"/try2think/section"+section+"/section"+section+".zip";
    Ion.with(Sections.context).load(downloadURL).write(new File(directory))
            .setCallback(new FutureCallback<File>() {
                @Override
                public void onCompleted(Exception e, File file) {
                    try {
                        UnzipFile(new File(directory), new File(Environment.getExternalStorageDirectory()+"/try2think/section"+section));
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
}

有什么问题?我尝试使用File folder = new File(Environment.getExternalStorageDirectory()+"/try2think/section"+section);变量,但它也没有用。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您是否尝试过添加写入外部存储空间的权限?

写入清单以下代码:

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

您还可以查看有关从Android存储中保存和阅读信息的有趣指南:Saving Files | Android Developers