diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 946cf278e..b3ee9f9db 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -505,21 +505,17 @@ public class Utils { return new File(context.getCacheDir() + "/" + name); } - public static File copyToTempFile(Context context, InputStream input, String name) { + public static File copyToTempFile(Context context, InputStream input, String name) throws IOException { File file = createTempFile(context, name); - try (FileOutputStream out = new FileOutputStream(file)) { - byte[] buf = new byte[4096]; - int len; - while ((len = input.read(buf)) != -1) { - out.write(buf, 0, len); - } - out.close(); - input.close(); - return file; - } catch (IOException e) { - Log.d("store temp file", "failed writing temp file, name: " + name); - return null; + FileOutputStream out = new FileOutputStream(file); + byte[] buf = new byte[4096]; + int len; + while ((len = input.read(buf)) != -1) { + out.write(buf, 0, len); } + out.close(); + input.close(); + return file; } public static String saveTempImage(Context context, Bitmap in, String name, Bitmap.CompressFormat format) { diff --git a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java index 78ee564b0..99eb3ef46 100644 --- a/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java @@ -7,6 +7,7 @@ import android.util.Log; import net.lingala.zip4j.exception.ZipException; import java.io.File; +import java.io.IOException; import java.io.InputStream; import protect.card_locker.Utils; @@ -46,25 +47,33 @@ public class MultiFormatImporter { String error = null; if (importer != null) { - File inputFile = Utils.copyToTempFile(context, input, TEMP_ZIP_NAME); - database.beginTransaction(); + File inputFile; try { - importer.importData(context, database, inputFile, password); - database.setTransactionSuccessful(); - return new ImportExportResult(ImportExportResultType.Success); - } catch (ZipException e) { - if (e.getType().equals(ZipException.Type.WRONG_PASSWORD)) { - return new ImportExportResult(ImportExportResultType.BadPassword); - } else { + inputFile = Utils.copyToTempFile(context, input, TEMP_ZIP_NAME); + database.beginTransaction(); + try { + importer.importData(context, database, inputFile, password); + database.setTransactionSuccessful(); + return new ImportExportResult(ImportExportResultType.Success); + } catch (ZipException e) { + if (e.getType().equals(ZipException.Type.WRONG_PASSWORD)) { + return new ImportExportResult(ImportExportResultType.BadPassword); + } else { + Log.e(TAG, "Failed to import data", e); + error = e.toString(); + } + } catch (Exception e) { Log.e(TAG, "Failed to import data", e); error = e.toString(); + } finally { + database.endTransaction(); + if (!inputFile.delete()) { + Log.w(TAG, "Failed to delete temporary ZIP file (should not be a problem) " + inputFile); + } } - } catch (Exception e) { - Log.e(TAG, "Failed to import data", e); + } catch (IOException e) { + Log.e(TAG, "Failed to copy ZIP file", e); error = e.toString(); - } finally { - database.endTransaction(); - inputFile.delete(); } } else { error = "Unsupported data format imported: " + format.name();