[FileSharing] update extensionCheck + display image

This commit is contained in:
Brieuc Viel 2017-08-17 11:50:35 +02:00
parent c5e90cf9df
commit c13dcd0768
2 changed files with 86 additions and 81 deletions

View file

@ -1127,8 +1127,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
if(fileSharedUri != null){
//save SipUri into bundle
onSaveInstanceState(getArguments());
String extension = LinphoneUtils.getExtensionFromFileName(fileSharedUri);
if(extension != null && extension.matches(".*(.png|.jpg|.jpeg|.bmp|.gif).*")) {
if(LinphoneUtils.isExtensionImage(fileSharedUri)) {
sendImageMessage(fileSharedUri, 0);
}else {
sendFileSharingMessage(fileSharedUri, 0);
@ -1633,8 +1632,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
public void loadBitmap(String path, ImageView imageView) {
if (cancelPotentialWork(path, imageView)) {
String extension = LinphoneUtils.getExtensionFromFileName(path);
if(extension != null && extension.matches(".*(.png|.jpg|.jpeg|.bmp|.gif).*"))
if(LinphoneUtils.isExtensionImage(path))
defaultBitmap = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.chat_picture_over);
else
defaultBitmap = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.chat_attachment_over);
@ -1663,47 +1661,50 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
Bitmap bm = null;
Bitmap thumbnail = null;
if (path.startsWith("content")) {
try {
bm = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(path));
} catch (FileNotFoundException e) {
Log.e(e);
} catch (IOException e) {
Log.e(e);
}
} else {
bm = BitmapFactory.decodeFile(path);
}
// Rotate the bitmap if possible/needed, using EXIF data
try {
Bitmap bm_tmp;
ExifInterface exif = new ExifInterface(path);
int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (pictureOrientation == 6) {
matrix.postRotate(90);
} else if (pictureOrientation == 3) {
matrix.postRotate(180);
} else if (pictureOrientation == 8) {
matrix.postRotate(270);
}
bm_tmp = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
if (bm_tmp != bm) {
bm.recycle();
bm = bm_tmp;
if(LinphoneUtils.isExtensionImage(path)) {
if (path.startsWith("content")) {
try {
bm = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(path));
} catch (FileNotFoundException e) {
Log.e(e);
} catch (IOException e) {
Log.e(e);
}
} else {
bm_tmp = null;
bm = BitmapFactory.decodeFile(path);
}
} catch (Exception e) {
Log.e(e);
}
if (bm != null) {
thumbnail = ThumbnailUtils.extractThumbnail(bm, SIZE_SMALL, SIZE_SMALL);
bm.recycle();
}
return thumbnail;
// Rotate the bitmap if possible/needed, using EXIF data
try {
Bitmap bm_tmp;
ExifInterface exif = new ExifInterface(path);
int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (pictureOrientation == 6) {
matrix.postRotate(90);
} else if (pictureOrientation == 3) {
matrix.postRotate(180);
} else if (pictureOrientation == 8) {
matrix.postRotate(270);
}
bm_tmp = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
if (bm_tmp != bm) {
bm.recycle();
bm = bm_tmp;
} else {
bm_tmp = null;
}
} catch (Exception e) {
Log.e(e);
}
if (bm != null) {
thumbnail = ThumbnailUtils.extractThumbnail(bm, SIZE_SMALL, SIZE_SMALL);
bm.recycle();
}
return thumbnail;
}else
return defaultBitmap;
}
// Once complete, see if ImageView is still around and set bitmap.
@ -1712,7 +1713,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
@ -1736,7 +1736,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
File file = new File(imageUri);
contentUri = FileProvider.getUriForFile(getActivity(), "org.linphone.provider", file);
}
intent.setDataAndType(contentUri, "image/*");
intent.setDataAndType(contentUri, "*/*");
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}

View file

@ -18,43 +18,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.linphone;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.linphone.core.DialPlan;
import org.linphone.core.LinphoneAccountCreator;
import org.linphone.core.LinphoneAddress;
import org.linphone.core.LinphoneCall;
import org.linphone.core.LinphoneCall.State;
import org.linphone.core.LinphoneChatMessage;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneCoreException;
import org.linphone.core.LinphoneCoreFactory;
import org.linphone.core.LinphoneProxyConfig;
import org.linphone.mediastream.Log;
import org.linphone.mediastream.video.capture.hwconf.Hacks;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
@ -81,6 +44,43 @@ import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import org.linphone.core.DialPlan;
import org.linphone.core.LinphoneAccountCreator;
import org.linphone.core.LinphoneAddress;
import org.linphone.core.LinphoneCall;
import org.linphone.core.LinphoneCall.State;
import org.linphone.core.LinphoneChatMessage;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneCoreException;
import org.linphone.core.LinphoneCoreFactory;
import org.linphone.core.LinphoneProxyConfig;
import org.linphone.mediastream.Log;
import org.linphone.mediastream.video.capture.hwconf.Hacks;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
/**
* Helpers.
* @author Guillaume Beraudo
@ -481,6 +481,11 @@ public final class LinphoneUtils {
return extension;
}
public static Boolean isExtensionImage(String path){
String extension = LinphoneUtils.getExtensionFromFileName(path);
return (extension != null && extension.matches(".*(png|jpg|jpeg|bmp|gif).*"));
}
public static void recursiveFileRemoval(File root) {
if (!root.delete()) {
if (root.isDirectory()) {