mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Fix resource leak bug (#5251)
Signed-off-by: Alfusainey Jallow <alf.jallow@gmail.com>
This commit is contained in:
parent
532e5375c2
commit
368e1c753d
1 changed files with 8 additions and 11 deletions
|
|
@ -56,18 +56,13 @@ public class PickedFiles implements Constants {
|
||||||
* @param in input stream of source file.
|
* @param in input stream of source file.
|
||||||
* @param file destination file
|
* @param file destination file
|
||||||
*/
|
*/
|
||||||
private static void writeToFile(InputStream in, File file) {
|
private static void writeToFile(InputStream in, File file) throws IOException {
|
||||||
try {
|
try (OutputStream out = new FileOutputStream(file)) {
|
||||||
OutputStream out = new FileOutputStream(file);
|
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
int len;
|
int len;
|
||||||
while ((len = in.read(buf)) > 0) {
|
while ((len = in.read(buf)) > 0) {
|
||||||
out.write(buf, 0, len);
|
out.write(buf, 0, len);
|
||||||
}
|
}
|
||||||
out.close();
|
|
||||||
in.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,9 +74,10 @@ public class PickedFiles implements Constants {
|
||||||
* @throws IOException (File input stream exception)
|
* @throws IOException (File input stream exception)
|
||||||
*/
|
*/
|
||||||
private static void copyFile(File src, File dst) throws IOException {
|
private static void copyFile(File src, File dst) throws IOException {
|
||||||
InputStream in = new FileInputStream(src);
|
try (InputStream in = new FileInputStream(src)) {
|
||||||
writeToFile(in, dst);
|
writeToFile(in, dst);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy files in separate thread.
|
* Copy files in separate thread.
|
||||||
|
|
@ -157,11 +153,12 @@ public class PickedFiles implements Constants {
|
||||||
* @return Uploadable file ready for tag redaction.
|
* @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
|
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 directory = tempImageDirectory(context);
|
||||||
File photoFile = new File(directory, UUID.randomUUID().toString() + "." + getMimeType(context, photoUri));
|
File photoFile = new File(directory, UUID.randomUUID().toString() + "." + getMimeType(context, photoUri));
|
||||||
if (photoFile.createNewFile()) {
|
if (photoFile.createNewFile()) {
|
||||||
|
try (InputStream pictureInputStream = context.getContentResolver().openInputStream(photoUri)) {
|
||||||
writeToFile(pictureInputStream, photoFile);
|
writeToFile(pictureInputStream, photoFile);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IOException("could not create photoFile to write upon");
|
throw new IOException("could not create photoFile to write upon");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue