Reputation:
I keep getting a file not found error, even though it exists at /mnt/sdcard/folder/Samples.zip. I downloaded the file there, so there are "write" permissions, but it just doesn't want to unzip! Why is this?
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;
}
Upvotes: 1
Views: 1173
Reputation: 40357
If you know the exact path, try as a test temporarily skipping the getExternalStorageDirectory() and just hard coding the path, and see if it works.
Then try using the function to discover the directory but write the discovered path name to logcat and see if it is as expected.
Beware that the Samsung Galaxy handles this a bit differently from other android devices, in that the function does not return the path to the removable sdcard.
Also the path name that is returned will be different between froyo and earlier versions - the reason you call the function is for protection from changes like that.
(I assume you have write external storage permission in this application, and not just in something else that did the download)
Upvotes: 0
Reputation: 161
Try replacing
in = new ZipInputStream(getAssets().open(
Environment.getExternalStorageDirectory() + zipPath));
with
in = new ZipInputStream(new FileInputStream(
Environment.getExternalStorageDirectory() + zipPath));
Context.getAssets() returns an AssetManager, which is normally used to access files in your project's /assets folder, not external files.
(Also, if you have your phone connected to your PC with a USB cable, it might not be allowing access to your SD Card. Try unplugging from USB when you run your app. Might just be an issue with my phone, though.)
Upvotes: 2
Reputation: 48577
You're using the wrong method.
getAssets().open() is defined as this
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.
Upvotes: 2
Reputation: 28665
try
Environment.getExternalStorageDirectory().getAbsolutePath()
instead of just
Environment.getExternalStorageDirectory()
Upvotes: 0