fixed #105
This commit is contained in:
parent
81d2760505
commit
f4eebd091c
|
@ -131,4 +131,9 @@
|
||||||
<string name="attach_choose_picture">Choose picture</string>
|
<string name="attach_choose_picture">Choose picture</string>
|
||||||
<string name="attach_take_picture">Take picture</string>
|
<string name="attach_take_picture">Take picture</string>
|
||||||
<string name="preemptively_grant">Preemptively grant subscription request</string>
|
<string name="preemptively_grant">Preemptively grant subscription request</string>
|
||||||
|
<string name="error_not_an_image_file">The file you selected is not an image</string>
|
||||||
|
<string name="error_compressing_image">Error while converting the image file</string>
|
||||||
|
<string name="error_file_not_found">File not found</string>
|
||||||
|
<string name="error_io_exception">General I/O error. Maybe you ran out of storage space?</string>
|
||||||
|
<string name="error_security_exception_during_image_copy">The app you used to select this image did not provide us with enough permissions to read the file.\n\n<small>Use a different file manager to choose an image</small></string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
@ -7,7 +7,6 @@ import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
@ -15,13 +14,9 @@ import android.graphics.BitmapFactory;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.LruCache;
|
import android.util.LruCache;
|
||||||
import android.view.View;
|
import eu.siacs.conversations.R;
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import eu.siacs.conversations.entities.Account;
|
|
||||||
import eu.siacs.conversations.entities.Conversation;
|
import eu.siacs.conversations.entities.Conversation;
|
||||||
import eu.siacs.conversations.entities.Message;
|
import eu.siacs.conversations.entities.Message;
|
||||||
import eu.siacs.conversations.services.XmppConnectionService;
|
|
||||||
import eu.siacs.conversations.xmpp.jingle.JingleFile;
|
import eu.siacs.conversations.xmpp.jingle.JingleFile;
|
||||||
|
|
||||||
public class FileBackend {
|
public class FileBackend {
|
||||||
|
@ -87,15 +82,13 @@ public class FileBackend {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JingleFile copyImageToPrivateStorage(Message message, Uri image) {
|
public JingleFile copyImageToPrivateStorage(Message message, Uri image)
|
||||||
|
throws ImageCopyException {
|
||||||
try {
|
try {
|
||||||
InputStream is;
|
InputStream is;
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
Log.d("xmppService","copying file: "+image.toString()+ " to internal storage");
|
is = context.getContentResolver().openInputStream(image);
|
||||||
is = context.getContentResolver()
|
|
||||||
.openInputStream(image);
|
|
||||||
} else {
|
} else {
|
||||||
Log.d("xmppService","copying file from incoming to internal storage");
|
|
||||||
is = new FileInputStream(getIncomingFile());
|
is = new FileInputStream(getIncomingFile());
|
||||||
}
|
}
|
||||||
JingleFile file = getJingleFile(message);
|
JingleFile file = getJingleFile(message);
|
||||||
|
@ -103,16 +96,19 @@ public class FileBackend {
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
OutputStream os = new FileOutputStream(file);
|
OutputStream os = new FileOutputStream(file);
|
||||||
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
|
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
|
||||||
|
if (originalBitmap == null) {
|
||||||
|
os.close();
|
||||||
|
throw new ImageCopyException(R.string.error_not_an_image_file);
|
||||||
|
}
|
||||||
is.close();
|
is.close();
|
||||||
if (image == null) {
|
if (image == null) {
|
||||||
Log.d("xmppService","delete incoming file");
|
|
||||||
getIncomingFile().delete();
|
getIncomingFile().delete();
|
||||||
}
|
}
|
||||||
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
|
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
|
||||||
boolean success = scalledBitmap.compress(
|
boolean success = scalledBitmap.compress(
|
||||||
Bitmap.CompressFormat.WEBP, 75, os);
|
Bitmap.CompressFormat.WEBP, 75, os);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
return null;
|
throw new ImageCopyException(R.string.error_compressing_image);
|
||||||
}
|
}
|
||||||
os.flush();
|
os.flush();
|
||||||
os.close();
|
os.close();
|
||||||
|
@ -122,11 +118,12 @@ public class FileBackend {
|
||||||
message.setBody("" + size + "," + width + "," + height);
|
message.setBody("" + size + "," + width + "," + height);
|
||||||
return file;
|
return file;
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
return null;
|
throw new ImageCopyException(R.string.error_file_not_found);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
throw new ImageCopyException(R.string.error_io_exception);
|
||||||
} catch (SecurityException e) {
|
} catch (SecurityException e) {
|
||||||
return null;
|
throw new ImageCopyException(
|
||||||
|
R.string.error_security_exception_during_image_copy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,4 +171,17 @@ public class FileBackend {
|
||||||
public File getIncomingFile() {
|
public File getIncomingFile() {
|
||||||
return new File(context.getFilesDir().getAbsolutePath() + "/incoming");
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -476,16 +476,15 @@ public class XmppConnectionService extends Service {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
JingleFile file = getFileBackend().copyImageToPrivateStorage(
|
try {
|
||||||
message, uri);
|
getFileBackend().copyImageToPrivateStorage(message, uri);
|
||||||
if (file == null) {
|
|
||||||
callback.error(R.string.error_copying_image_file);
|
|
||||||
} else {
|
|
||||||
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
|
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
|
||||||
getPgpEngine().encrypt(message, callback);
|
getPgpEngine().encrypt(message, callback);
|
||||||
} else {
|
} else {
|
||||||
callback.success();
|
callback.success();
|
||||||
}
|
}
|
||||||
|
} catch (FileBackend.ImageCopyException e) {
|
||||||
|
callback.error(e.getResId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
Loading…
Reference in New Issue