delete targe file after unsuccessful image compression

This commit is contained in:
Daniel Gultsch 2021-09-11 09:55:44 +02:00
parent c195e8b3d2
commit 68d8e2b9cf
1 changed files with 32 additions and 19 deletions

View File

@ -33,6 +33,8 @@ import androidx.annotation.StringRes;
import androidx.core.content.FileProvider;
import androidx.exifinterface.media.ExifInterface;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
@ -627,37 +629,38 @@ public class FileBackend {
private void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
Log.d(Config.LOGTAG, "copy file (" + uri.toString() + ") to private storage " + file.getAbsolutePath());
file.getParentFile().mkdirs();
OutputStream os = null;
InputStream is = null;
try {
file.createNewFile();
os = new FileOutputStream(file);
is = mXmppConnectionService.getContentResolver().openInputStream(uri);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
} catch (IOException e) {
throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
}
try (final OutputStream os = new FileOutputStream(file);
final InputStream is = mXmppConnectionService.getContentResolver().openInputStream(uri)) {
if (is == null) {
throw new FileCopyException(R.string.error_file_not_found);
}
try {
os.write(buffer, 0, length);
ByteStreams.copy(is, os);
} catch (IOException e) {
throw new FileWriterException();
}
}
try {
os.flush();
} catch (IOException e) {
throw new FileWriterException();
}
} catch (final FileNotFoundException e) {
cleanup(file);
throw new FileCopyException(R.string.error_file_not_found);
} catch (final FileWriterException e) {
cleanup(file);
throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
} catch (final SecurityException e) {
cleanup(file);
throw new FileCopyException(R.string.error_security_exception);
} catch (final IOException e) {
cleanup(file);
throw new FileCopyException(R.string.error_io_exception);
} finally {
close(os);
close(is);
}
}
@ -708,7 +711,7 @@ public class FileBackend {
private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, ImageCompressionException {
final File parent = file.getParentFile();
if (parent.mkdirs()) {
if (parent != null && parent.mkdirs()) {
Log.d(Config.LOGTAG, "created parent directory");
}
InputStream is = null;
@ -753,13 +756,15 @@ public class FileBackend {
}
scaledBitmap.recycle();
} catch (final FileNotFoundException e) {
cleanup(file);
throw new FileCopyException(R.string.error_file_not_found);
} catch (IOException e) {
e.printStackTrace();
} catch (final IOException e) {
cleanup(file);
throw new FileCopyException(R.string.error_io_exception);
} catch (SecurityException e) {
cleanup(file);
throw new FileCopyException(R.string.error_security_exception_during_image_copy);
} catch (OutOfMemoryError e) {
} catch (final OutOfMemoryError e) {
++sampleSize;
if (sampleSize <= 3) {
copyImageToPrivateStorage(file, image, sampleSize);
@ -772,6 +777,14 @@ public class FileBackend {
}
}
private static void cleanup(final File file) {
try {
file.delete();
} catch (Exception e) {
}
}
public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException, ImageCompressionException {
Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
copyImageToPrivateStorage(file, image, 0);