diff options
Diffstat (limited to 'src/eu/siacs/conversations/persistance/FileBackend.java')
-rw-r--r-- | src/eu/siacs/conversations/persistance/FileBackend.java | 64 |
1 files changed, 44 insertions, 20 deletions
diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java index becb1ee3..3a580048 100644 --- a/src/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/eu/siacs/conversations/persistance/FileBackend.java @@ -1,12 +1,12 @@ package eu.siacs.conversations.persistance; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.ref.WeakReference; import android.content.Context; import android.graphics.Bitmap; @@ -14,14 +14,9 @@ import android.graphics.BitmapFactory; import android.net.Uri; import android.util.Log; import android.util.LruCache; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; - -import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; -import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.xmpp.jingle.JingleFile; public class FileBackend { @@ -43,11 +38,11 @@ public class FileBackend { }; } - + public LruCache<String, Bitmap> getThumbnailCache() { return thumbnailCache; } - + public JingleFile getJingleFile(Message message) { return getJingleFile(message, true); } @@ -58,7 +53,7 @@ public class FileBackend { String path = prefix + "/" + conversation.getAccount().getJid() + "/" + conversation.getContactJid(); String filename; - if ((decrypted)||(message.getEncryption() == Message.ENCRYPTION_NONE)) { + if ((decrypted) || (message.getEncryption() == Message.ENCRYPTION_NONE)) { filename = message.getUuid() + ".webp"; } else { filename = message.getUuid() + ".webp.pgp"; @@ -87,36 +82,48 @@ public class FileBackend { } } - public JingleFile copyImageToPrivateStorage(Message message, Uri image) { + public JingleFile copyImageToPrivateStorage(Message message, Uri image) + throws ImageCopyException { try { - Log.d("xmppService","copying file: "+image.toString()+ " to internal storage"); - InputStream is = context.getContentResolver() - .openInputStream(image); + InputStream is; + if (image != null) { + is = context.getContentResolver().openInputStream(image); + } else { + is = new FileInputStream(getIncomingFile()); + } JingleFile file = getJingleFile(message); file.getParentFile().mkdirs(); file.createNewFile(); OutputStream os = new FileOutputStream(file); Bitmap originalBitmap = BitmapFactory.decodeStream(is); + if (originalBitmap == null) { + os.close(); + throw new ImageCopyException(R.string.error_not_an_image_file); + } is.close(); + if (image == null) { + getIncomingFile().delete(); + } Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE); boolean success = scalledBitmap.compress( Bitmap.CompressFormat.WEBP, 75, os); if (!success) { - return null; + throw new ImageCopyException(R.string.error_compressing_image); } os.flush(); os.close(); long size = file.getSize(); int width = scalledBitmap.getWidth(); int height = scalledBitmap.getHeight(); - message.setBody(""+size+","+width+","+height); + message.setBody("" + size + "," + width + "," + height); return file; } catch (FileNotFoundException e) { - return null; + throw new ImageCopyException(R.string.error_file_not_found); } catch (IOException e) { - return null; + throw new ImageCopyException(R.string.error_io_exception); } catch (SecurityException e) { - return null; + throw new ImageCopyException( + R.string.error_security_exception_during_image_copy); } } @@ -128,7 +135,7 @@ public class FileBackend { public Bitmap getThumbnail(Message message, int size, boolean cacheOnly) throws FileNotFoundException { Bitmap thumbnail = thumbnailCache.get(message.getUuid()); - if ((thumbnail == null)&&(!cacheOnly)) { + if ((thumbnail == null) && (!cacheOnly)) { Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message) .getAbsolutePath()); if (fullsize == null) { @@ -160,4 +167,21 @@ public class FileBackend { } f.delete(); } + + public File getIncomingFile() { + return new File(context.getFilesDir().getAbsolutePath() + "/incoming"); + } + + public class ImageCopyException extends Exception { + private static final long serialVersionUID = -1010013599132881427L; + private int resId; + + public ImageCopyException(int resId) { + this.resId = resId; + } + + public int getResId() { + return resId; + } + } } |