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

View file

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