最近,我读到了android中的文件夹创建,并尝试在我的应用程序中实现它。函数应该使dir,如果它不存在,从webserver下载文件并解压缩。但应用程序无法在外部设备上创建文件夹。函数代码
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);的变体,但它也不起作用。我做错了什么?
发布于 2016-03-08 20:12:55
您是否尝试添加写外部存储的权限?
将以下代码写入清单:
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application ...
</manifest>你也可以看看这个有趣的关于从android存储中保存和读取信息的指南:Saving Files | Android Developers
https://stackoverflow.com/questions/35866946
复制相似问题