changed FileBackend API to allow files instead of messages to be copied and resized

This commit is contained in:
Daniel Gultsch 2016-01-24 12:17:00 +01:00
parent 8850a1fbe3
commit 31fd425c9a

View file

@ -86,7 +86,7 @@ public class FileBackend {
public static String getConversationsImageDirectory() {
return Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath()
Environment.DIRECTORY_PICTURES).getAbsolutePath()
+ "/Conversations/";
}
@ -155,12 +155,7 @@ public class FileBackend {
return FileUtils.getPath(mXmppConnectionService,uri);
}
public DownloadableFile copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
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);
public void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
file.getParentFile().mkdirs();
OutputStream os = null;
InputStream is = null;
@ -183,28 +178,18 @@ public class FileBackend {
close(os);
close(is);
}
Log.d(Config.LOGTAG, "output file name " + mXmppConnectionService.getFileBackend().getFile(message));
return file;
Log.d(Config.LOGTAG, "output file name " + file.getAbsolutePath());
}
public DownloadableFile copyImageToPrivateStorage(Message message, Uri image)
throws FileCopyException {
return this.copyImageToPrivateStorage(message, image, 0);
public void copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
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);
copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
}
private DownloadableFile copyImageToPrivateStorage(Message message,Uri image, int sampleSize) throws FileCopyException {
switch(Config.IMAGE_FORMAT) {
case JPEG:
message.setRelativeFilePath(message.getUuid()+".jpg");
break;
case PNG:
message.setRelativeFilePath(message.getUuid()+".png");
break;
case WEBP:
message.setRelativeFilePath(message.getUuid()+".webp");
break;
}
DownloadableFile file = getFile(message);
private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException {
file.getParentFile().mkdirs();
InputStream is = null;
OutputStream os = null;
@ -225,7 +210,6 @@ public class FileBackend {
int rotation = getRotation(image);
scaledBitmap = rotate(scaledBitmap, rotation);
boolean targetSizeReached = false;
long size = 0;
int quality = Config.IMAGE_QUALITY;
while(!targetSizeReached) {
os = new FileOutputStream(file);
@ -234,14 +218,11 @@ public class FileBackend {
throw new FileCopyException(R.string.error_compressing_image);
}
os.flush();
size = file.getSize();
targetSizeReached = size <= Config.IMAGE_MAX_SIZE || quality <= 50;
targetSizeReached = file.length() <= Config.IMAGE_MAX_SIZE || quality <= 50;
quality -= 5;
}
int width = scaledBitmap.getWidth();
int height = scaledBitmap.getHeight();
message.setBody(Long.toString(size) + '|' + width + '|' + height);
return file;
scaledBitmap.recycle();
return;
} catch (FileNotFoundException e) {
throw new FileCopyException(R.string.error_file_not_found);
} catch (IOException e) {
@ -252,7 +233,7 @@ public class FileBackend {
} catch (OutOfMemoryError e) {
++sampleSize;
if (sampleSize <= 3) {
return copyImageToPrivateStorage(message, image, sampleSize);
copyImageToPrivateStorage(file, image, sampleSize);
} else {
throw new FileCopyException(R.string.error_out_of_memory);
}
@ -264,6 +245,26 @@ public class FileBackend {
}
}
public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
copyImageToPrivateStorage(file, image, 0);
}
public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException {
switch(Config.IMAGE_FORMAT) {
case JPEG:
message.setRelativeFilePath(message.getUuid()+".jpg");
break;
case PNG:
message.setRelativeFilePath(message.getUuid()+".png");
break;
case WEBP:
message.setRelativeFilePath(message.getUuid()+".webp");
break;
}
copyImageToPrivateStorage(getFile(message), image);
updateFileParams(message);
}
private int getRotation(File file) {
return getRotation(Uri.parse("file://"+file.getAbsolutePath()));
}