在我的应用中解压缩包含图片的文件

时间:2018-03-08 15:34:12

标签: java android

我开发了一个应用程序,用于备份和恢复应用数据。备份数据以.zip格式存储在电话簿中。恢复时,我解压缩文件直到这里一切正常。但是,如果备份包含图像作为数据,则恢复时它们不会在应用程序中打开并发出错误。为解压缩文件编写的代码写为

 public void DBimport(String inFileName) {

       opener= new DataBaseOpener(mApp);
       final String outFileName = mContext.getDatabasePath(opener.getDatabaseName()).toString();

       try {

           File dbFile = new File(inFileName);
           FileInputStream fis = new FileInputStream(dbFile);
           ZipInputStream zipInputStream= new ZipInputStream(fis);
           ZipEntry zipEntry= zipInputStream.getNextEntry();

           while(zipEntry!=null) {
               String fileName=zipEntry.getName();
               File newFile= new File(outFileName/* + File.separator + fileName*/);

               new File(newFile.getParent()).mkdirs();

               // Open the empty db as the output stream
               OutputStream output = new FileOutputStream(newFile);

               // Transfer bytes from the input file to the output file
               byte[] buffer = new byte[1024];
               int length;
               while ((length = zipInputStream.read(buffer)) > 0) {
                   output.write(buffer, 0, length);
               }
               // Close the streams
               output.flush();
               output.close();
               zipEntry=zipInputStream.getNextEntry();
           }


           zipInputStream.closeEntry();

           fis.close();

           Toast.makeText(mContext, "Restore Completed", Toast.LENGTH_LONG).show();

       } catch (IOException e) {
           Toast.makeText(mContext, "Unable to Restores. Retry", Toast.LENGTH_LONG).show();
           e.printStackTrace();
       } 
   }

}

我可以知道我哪里出错吗?以及如何解决此问题。任何建议都会有所帮助。

0 个答案:

没有答案