0
我想从sd卡解压zip文件到/ internal_Storage。它工作得很好,没有问题,成功完成成功。发生 的问题时,我突然拔出存储卡,然后我得到这个异常:Xamarin Android从SD卡解压缩文件,拔出System.IO.IOException异常后
未处理的异常:
System.IO.IOException:无效的句柄路径“/mnt/sdcard/Sounds.zip”
应用程序崩溃后。
string zipFile = @"/mnt/Sounds.zip";
string unZipSoundsFolderPath = @"/data/internal_Storage/Sounds/";
using (var zipInputStream = new ZipInputStream(System.IO.File.OpenRead(zipFile)))
{
ZipEntry ze = null;
try
{
while ((ze = zipInputStream.NextEntry) != null)
{
if (ze.IsDirectory)
{
if (!Directory.Exists(unZipSoundsFolderPath + ze.Name))
{
Directory.CreateDirectory(unZipSoundsFolderPath + ze.Name);
}
continue;
}
FileStream fout = new FileStream(unZipSoundsFolderPath + ze.Name, FileMode.Create);
BufferedStream bfout = new BufferedStream(fout);
try
{
byte[] buffer = new byte[2048];
int read = 0;
while ((read = zipInputStream.Read(buffer)) != -1) //THIS ROW DROPS EXCEPTION
{
bfout.Write(buffer, 0, read);
}
}
catch (System.IO.IOException exs)//HERE NOT CAUGHT EXCEPTION
{
break;
}
catch (Java.IO.IOException jex)//HERE NOT CAUGHT EXCEPTION
{
break;
}
zipInputStream.CloseEntry();
bfout.Close();
fout.Close();
}
}
catch (System.IO.IOException exs)
{
System.Diagnostics.Debug.WriteLine(exs.Message);
importIsSuccessful = false;
}
catch (Java.IO.IOException)
{
importIsSuccessful = false;
}
catch(Exception ex)
{
importIsSuccessful = false;
}
zipInputStream.Close();
}
我做了很多事情,但每当SD卡拔出时,应用程序都会粉碎。谢谢你的帮助!
谢谢你的回答。我在using语句中使用了try-catch块,从我的文章中错过了它。不幸的结果是相同的:System.IO.IOException:路径“/mnt/sdcard/Sounds.zip”的句柄无效。我留下了使用声明,保持try-catch,但异常没有被任何catch语句捕获。这是关键点:'read = zipInputStream.Read(buffer)'。 Zipinputstream失去了压缩文件的路径。任何try-catch都无法捕捉到这个异常,这很奇怪。 – user1223445
您需要掌握异常本身,以便您可以检查其堆栈跟踪。这将告诉你Exception从哪里抛出。 –