diff --git a/app/src/main/java/fr/free/nrw/commons/filepicker/PickedFiles.java b/app/src/main/java/fr/free/nrw/commons/filepicker/PickedFiles.java index ea59831731..ca1abba623 100644 --- a/app/src/main/java/fr/free/nrw/commons/filepicker/PickedFiles.java +++ b/app/src/main/java/fr/free/nrw/commons/filepicker/PickedFiles.java @@ -56,18 +56,13 @@ private static File tempImageDirectory(@NonNull Context context) { * @param in input stream of source file. * @param file destination file */ - private static void writeToFile(InputStream in, File file) { - try { - OutputStream out = new FileOutputStream(file); + private static void writeToFile(InputStream in, File file) throws IOException { + try (OutputStream out = new FileOutputStream(file)) { byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } - out.close(); - in.close(); - } catch (Exception e) { - e.printStackTrace(); } } @@ -79,8 +74,9 @@ private static void writeToFile(InputStream in, File file) { * @throws IOException (File input stream exception) */ private static void copyFile(File src, File dst) throws IOException { - InputStream in = new FileInputStream(src); - writeToFile(in, dst); + try (InputStream in = new FileInputStream(src)) { + writeToFile(in, dst); + } } /** @@ -157,11 +153,12 @@ static void scanCopiedImages(Context context, List copiedImages) { * @return Uploadable file ready for tag redaction. */ public static UploadableFile pickedExistingPicture(@NonNull Context context, Uri photoUri) throws IOException, SecurityException {// SecurityException for those file providers who share URI but forget to grant necessary permissions - InputStream pictureInputStream = context.getContentResolver().openInputStream(photoUri); File directory = tempImageDirectory(context); File photoFile = new File(directory, UUID.randomUUID().toString() + "." + getMimeType(context, photoUri)); if (photoFile.createNewFile()) { - writeToFile(pictureInputStream, photoFile); + try (InputStream pictureInputStream = context.getContentResolver().openInputStream(photoUri)) { + writeToFile(pictureInputStream, photoFile); + } } else { throw new IOException("could not create photoFile to write upon"); }