aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/persistance
diff options
context:
space:
mode:
authoriNPUTmice <daniel@gultsch.de>2014-11-14 00:28:39 +0100
committeriNPUTmice <daniel@gultsch.de>2014-11-15 15:34:11 +0100
commitdac12be53e18962c80fd471421adac29b5c92f06 (patch)
tree554839625546e0bd6e36361a199fc810389a0e76 /src/main/java/eu/siacs/conversations/persistance
parent02cbda68a7af5c500f2044cb97507c680ef2bc41 (diff)
copy non local files to private storage first
Diffstat (limited to 'src/main/java/eu/siacs/conversations/persistance')
-rw-r--r--src/main/java/eu/siacs/conversations/persistance/FileBackend.java32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
index 2d054b5ec..908cbefc1 100644
--- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
+++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
@@ -29,6 +29,8 @@ import android.provider.MediaStore;
import android.util.Base64;
import android.util.Base64OutputStream;
import android.util.Log;
+import android.webkit.MimeTypeMap;
+
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.DownloadableFile;
@@ -118,8 +120,8 @@ public class FileBackend {
public String getOriginalPath(Uri uri) {
String path = null;
if (uri.getScheme().equals("file")) {
- path = uri.getPath();
- } else {
+ return uri.getPath();
+ } else if (uri.toString().startsWith("content://media/")) {
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor metaCursor = mXmppConnectionService.getContentResolver().query(uri,
projection, null, null, null);
@@ -136,6 +138,32 @@ public class FileBackend {
return path;
}
+ public DownloadableFile copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
+ try {
+ Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage");
+ String mime = mXmppConnectionService.getContentResolver().getType(uri);
+ String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
+ message.setRelativeFilePath(message.getUuid() + "." + extension);
+ DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
+ OutputStream os = new FileOutputStream(file);
+ InputStream is = mXmppConnectionService.getContentResolver().openInputStream(uri);
+ byte[] buffer = new byte[1024];
+ int length;
+ while ((length = is.read(buffer)) > 0) {
+ os.write(buffer, 0, length);
+ }
+ os.flush();
+ os.close();
+ is.close();
+ Log.d(Config.LOGTAG, "output file name " + mXmppConnectionService.getFileBackend().getFile(message));
+ return file;
+ } catch (FileNotFoundException e) {
+ throw new FileCopyException(R.string.error_file_not_found);
+ } catch (IOException e) {
+ throw new FileCopyException(R.string.error_io_exception);
+ }
+ }
+
public DownloadableFile copyImageToPrivateStorage(Message message, Uri image)
throws FileCopyException {
return this.copyImageToPrivateStorage(message, image, 0);