aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/siacs/conversations/persistance/FileBackend.java')
-rw-r--r--src/main/java/eu/siacs/conversations/persistance/FileBackend.java38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
index 921ac12a..84330d16 100644
--- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
+++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
@@ -54,12 +54,13 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.ExifHelper;
import eu.siacs.conversations.utils.FileUtils;
+import eu.siacs.conversations.utils.FileWriterException;
import eu.siacs.conversations.xmpp.pep.Avatar;
public class FileBackend {
private static final SimpleDateFormat IMAGE_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
- public static final String CONVERSATIONS_FILE_PROVIDER = "eu.siacs.conversations.files";
+ private static final String FILE_PROVIDER = ".files";
private XmppConnectionService mXmppConnectionService;
@@ -250,11 +251,21 @@ public class FileBackend {
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
- os.write(buffer, 0, length);
+ try {
+ os.write(buffer, 0, length);
+ } catch (IOException e) {
+ throw new FileWriterException();
+ }
+ }
+ try {
+ os.flush();
+ } catch (IOException e) {
+ throw new FileWriterException();
}
- os.flush();
} catch(FileNotFoundException e) {
throw new FileCopyException(R.string.error_file_not_found);
+ } catch(FileWriterException e) {
+ throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
} catch (IOException e) {
e.printStackTrace();
throw new FileCopyException(R.string.error_io_exception);
@@ -299,8 +310,13 @@ public class FileBackend {
InputStream is = null;
OutputStream os = null;
try {
- file.createNewFile();
+ if (!file.exists() && !file.createNewFile()) {
+ throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
+ }
is = mXmppConnectionService.getContentResolver().openInputStream(image);
+ if (is == null) {
+ throw new FileCopyException(R.string.error_not_an_image_file);
+ }
Bitmap originalBitmap;
BitmapFactory.Options options = new BitmapFactory.Options();
int inSampleSize = (int) Math.pow(2, sampleSize);
@@ -327,7 +343,6 @@ public class FileBackend {
quality -= 5;
}
scaledBitmap.recycle();
- return;
} catch (FileNotFoundException e) {
throw new FileCopyException(R.string.error_file_not_found);
} catch (IOException e) {
@@ -342,8 +357,6 @@ public class FileBackend {
} else {
throw new FileCopyException(R.string.error_out_of_memory);
}
- } catch (NullPointerException e) {
- throw new FileCopyException(R.string.error_io_exception);
} finally {
close(os);
close(is);
@@ -458,12 +471,17 @@ public class FileBackend {
}
file.getParentFile().mkdirs();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
- return FileProvider.getUriForFile(mXmppConnectionService, CONVERSATIONS_FILE_PROVIDER, file);
+ return getUriForFile(mXmppConnectionService,file);
} else {
return Uri.fromFile(file);
}
}
+ public static Uri getUriForFile(Context context, File file) {
+ String packageId = context.getPackageName();
+ return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file);
+ }
+
public static Uri getIndexableTakePhotoUri(Uri original) {
if (Config.ONLY_INTERNAL_STORAGE || "file".equals(original.getScheme())) {
return original;
@@ -711,9 +729,7 @@ public class FileBackend {
}
public Uri getJingleFileUri(Message message) {
- File file = getFile(message);
- return FileProvider.getUriForFile(mXmppConnectionService,
- CONVERSATIONS_FILE_PROVIDER, file);
+ return getUriForFile(mXmppConnectionService,getFile(message));
}
public void updateFileParams(Message message) {