Set correct content type when uploading image + store image as correct type (fix png transparency issue)

This commit is contained in:
Sylvain Berfini 2016-07-18 15:33:47 +02:00
parent bffe9c9537
commit 107fc4b54f
6 changed files with 60 additions and 20 deletions

View file

@ -68,10 +68,8 @@
<string name="about_bugreport_email">linphone-android@belledonne-communications.com</string>
<bool name="use_linphonecore_ringing">false</bool>
<string name="temp_photo_name">linphone-android-photo-temp.jpg</string>
<string name="temp_photo_name_with_date">linphone-android-photo-%s.jpg</string>
<string name="temp_photo_name">linphone-android-photo-temp</string>
<string name="temp_photo_name_with_date">linphone-android-photo-%s</string>
<bool name="hide_phone_numbers_in_editor">false</bool>
<bool name="hide_sip_addresses_in_editor">false</bool>

View file

@ -20,7 +20,6 @@
<string name="messages_date_format">dd/MM, HH:mm</string>
<string name="messages_list_date_format">dd/MM</string>
<string name="today_date_format">HH:mm</string>
<string name="picture_name_format">linphone-mms-%s.jpg</string>
<!-- Common -->
<string name="username">Username</string>

View file

@ -710,7 +710,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
editList.setVisibility(View.VISIBLE);
isEditMode = true;
redrawMessageList();
//TODO refaire la liste
}
if(id == R.id.start_call){
LinphoneActivity.instance().setAddresGoToDialerAndCall(sipUri, LinphoneUtils.getUsernameFromAddress(sipUri), null);
@ -909,7 +908,12 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
String extension = LinphoneUtils.getExtensionFromFileName(path);
if (extension != null && extension.toLowerCase(Locale.getDefault()).equals("png")) {
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
} else {
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
}
byte[] byteArray = stream.toByteArray();
return byteArray;
}
@ -921,8 +925,9 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
}
mUploadingImageStream = new ByteArrayInputStream(result);
LinphoneContent content = LinphoneCoreFactory.instance().createLinphoneContent("image", "jpeg", result, null);
String fileName = path.substring(path.lastIndexOf("/") + 1);
String extension = LinphoneUtils.getExtensionFromFileName(fileName);
LinphoneContent content = LinphoneCoreFactory.instance().createLinphoneContent("image", extension, result, null);
content.setName(fileName);
LinphoneChatMessage message = chatRoom.createFileTransferMessage(content);

View file

@ -29,10 +29,12 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
@ -79,6 +81,7 @@ import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@ -102,6 +105,7 @@ import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.telephony.TelephonyManager;
@ -284,6 +288,40 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
public void setUploadingImageStream(ByteArrayInputStream array){
this.mUploadingImageStream = array;
}
private void storeImage(LinphoneChatMessage msg) {
if (msg == null || msg.getFileTransferInformation() == null || msg.getAppData() == null) return;
File file = new File(Environment.getExternalStorageDirectory(), msg.getAppData());
Bitmap bm = BitmapFactory.decodeFile(file.getPath());
if (bm == null) return;
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, file.getName());
String extension = msg.getFileTransferInformation().getSubtype();
values.put(Images.Media.MIME_TYPE, "image/" + extension);
ContentResolver cr = getContext().getContentResolver();
Uri path = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream stream;
try {
stream = cr.openOutputStream(path);
if (extension != null && extension.toLowerCase(Locale.getDefault()).equals("png")) {
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
} else {
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
}
stream.close();
file.delete();
bm.recycle();
msg.setAppData(path.toString());
} catch (FileNotFoundException e) {
Log.e(e);
} catch (IOException e) {
Log.e(e);
}
}
@Override
public void onLinphoneChatMessageStateChanged(LinphoneChatMessage msg, LinphoneChatMessage.State state) {
@ -292,17 +330,7 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
mUploadPendingFileMessage = null;
mUploadingImageStream = null;
} else {
File file = new File(Environment.getExternalStorageDirectory(), msg.getAppData());
try {
Bitmap bm = BitmapFactory.decodeFile(file.getPath());
if (bm != null) {
String url = MediaStore.Images.Media.insertImage(getContext().getContentResolver(), file.getPath(), file.getName(), null);
msg.setAppData(url);
file.delete();
}
} catch (FileNotFoundException e) {
Log.e(e);
}
storeImage(msg);
removePendingMessage(msg);
}
}

View file

@ -455,5 +455,14 @@ public final class LinphoneUtils {
Log.e(e);
}
}
public static String getExtensionFromFileName(String fileName) {
String extension = null;
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i+1);
}
return extension;
}
}

View file

@ -173,7 +173,8 @@ public class BubbleChat implements LinphoneChatMessage.LinphoneChatMessageListen
public void onClick(View v) {
if (mContext.getPackageManager().checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, mContext.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
v.setEnabled(false);
String filename = context.getString(R.string.temp_photo_name_with_date).replace("%s", String.valueOf(System.currentTimeMillis()));
String extension = nativeMessage.getFileTransferInformation().getSubtype();
String filename = context.getString(R.string.temp_photo_name_with_date).replace("%s", String.valueOf(System.currentTimeMillis())) + "." + extension;
File file = new File(Environment.getExternalStorageDirectory(), filename);
nativeMessage.setAppData(filename);
LinphoneManager.getInstance().addDownloadMessagePending(nativeMessage);