我一直收到文件未找到错误,即使它存在于/mnt/sdcard/folder/Samples.zip中。我在那里下载了文件,因此有“写”权限,但它只是不想解压缩!这是为什么?
ZipInputStream in = null;
try {
final String zipPath = "/folder/Samples.zip";
in = new ZipInputStream(getAssets().open(
Environment.getExternalStorageDirectory() + zipPath));
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in
.getNextEntry()) {
}
} catch (IOException e) {
System.out.println("file error of some kind" + e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ignored) {
}
in = null;
}
答案 0 :(得分:2)
您使用的是错误的方法。
getAssets()。open()定义为此
Open an asset using an explicit access mode, returning an InputStream to read its contents. This provides access to files that have been bundled with an application as assets -- that is, files placed in to the "assets" directory.
答案 1 :(得分:2)
尝试替换
in = new ZipInputStream(getAssets().open(
Environment.getExternalStorageDirectory() + zipPath));
与
in = new ZipInputStream(new FileInputStream(
Environment.getExternalStorageDirectory() + zipPath));
Context.getAssets()返回AssetManager,通常用于访问项目/ assets文件夹中的文件,而不是外部文件。
(此外,如果您使用USB线将手机连接到PC,则可能无法访问SD卡。尝试运行应用时请尝试从USB拔下。可能只是手机出现问题,虽然。)
答案 2 :(得分:0)
尝试
Environment.getExternalStorageDirectory().getAbsolutePath()
而不仅仅是
Environment.getExternalStorageDirectory()
答案 3 :(得分:0)
如果您知道确切的路径,请尝试暂时跳过getExternalStorageDirectory()并尝试对路径进行硬编码,然后查看它是否有效。
然后尝试使用该函数发现目录,但将发现的路径名写入logcat,看看它是否符合预期。
请注意,三星Galaxy与其他Android设备的处理方式略有不同,因为该功能不会返回可移动SD卡的路径。
froyo和早期版本之间返回的路径名也是不同的 - 你调用该函数的原因是为了防止这样的更改。
(我假设您已经在此应用程序中编写了外部存储权限,而不仅仅是在进行下载的其他内容中)