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