From 4d79111c96dc634cdd365ee6fb3c23cee63be422 Mon Sep 17 00:00:00 2001 From: Alexandru Date: Fri, 28 Feb 2014 16:35:27 +0100 Subject: [PATCH] fix sharing remote images from another application --- .../org/telegram/messenger/Utilities.java | 27 +++++++++++++++++ .../org/telegram/ui/ApplicationActivity.java | 8 ++++- .../java/org/telegram/ui/ChatActivity.java | 30 ++----------------- .../java/org/telegram/ui/LaunchActivity.java | 22 ++++---------- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/Utilities.java b/TMessagesProj/src/main/java/org/telegram/messenger/Utilities.java index 2eb549822..b43e35857 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/Utilities.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/Utilities.java @@ -15,11 +15,14 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Typeface; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.os.Handler; +import android.os.ParcelFileDescriptor; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.text.Html; @@ -37,7 +40,9 @@ import org.telegram.ui.ApplicationLoader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileDescriptor; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; @@ -814,6 +819,28 @@ public class Utilities { return null; } + public static Bitmap getBitmap(final Uri uri){ + Bitmap result = null; + ParcelFileDescriptor parcelFD = null; + try { + parcelFD = ApplicationLoader.applicationContext.getContentResolver().openFileDescriptor(uri, "r"); + FileDescriptor fileDescriptor = parcelFD.getFileDescriptor(); + result = BitmapFactory.decodeFileDescriptor(fileDescriptor); + } catch (FileNotFoundException e) { + FileLog.e("tmessages", e); + } catch (IOException e) { + FileLog.e("tmessages", e); + } finally { + if (parcelFD != null) + try { + parcelFD.close(); + } catch (IOException e) { + } + } + + return result; + } + public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ApplicationActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ApplicationActivity.java index 9c25274ee..255460913 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ApplicationActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ApplicationActivity.java @@ -13,6 +13,7 @@ import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Bitmap; import android.graphics.PixelFormat; import android.os.Build; import android.os.Bundle; @@ -49,6 +50,7 @@ public class ApplicationActivity extends ActionBarActivity implements Notificati private boolean finished = false; private NotificationView notificationView; private String photoPath = null; + private Bitmap sendingBitmap = null; private String videoPath = null; private String sendingText = null; private int currentConnectionState; @@ -217,7 +219,8 @@ public class ApplicationActivity extends ActionBarActivity implements Notificati photoPath = (String)NotificationCenter.Instance.getFromMemCache(533); videoPath = (String)NotificationCenter.Instance.getFromMemCache(534); sendingText = (String)NotificationCenter.Instance.getFromMemCache(535); - if (videoPath != null || photoPath != null || sendingText != null) { + sendingBitmap = (Bitmap)NotificationCenter.Instance.getFromMemCache(536); + if (videoPath != null || photoPath != null || sendingText != null || sendingBitmap != null) { MessagesActivity fragment = new MessagesActivity(); fragment.selectAlertString = R.string.ForwardMessagesTo; fragment.animationType = 1; @@ -310,10 +313,13 @@ public class ApplicationActivity extends ActionBarActivity implements Notificati fragment.processSendingVideo(videoPath); } else if (sendingText != null) { fragment.processSendingText(sendingText); + } else if (sendingBitmap != null) { + fragment.processSendingPhoto(sendingBitmap); } photoPath = null; videoPath = null; sendingText = null; + sendingBitmap = null; } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 4d344dcdd..73bc4a1f0 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -1242,34 +1242,8 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa return; } String imageFilePath = null; - if (imageUri.getScheme().contains("file")) { - imageFilePath = imageUri.getPath(); - processSendingPhoto(imageFilePath); - } else if(imageUri.getScheme().contains("content")){ - ParcelFileDescriptor parcelFD = null; - try { - parcelFD = parentActivity.getContentResolver().openFileDescriptor(imageUri, "r"); - FileDescriptor fileDescriptor = parcelFD.getFileDescriptor(); - Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); - processSendingPhoto(bitmap); - } catch (FileNotFoundException e) { - return; - } catch (IOException e) { - return; - } finally { - if (parcelFD != null) - try { - parcelFD.close(); - } catch (IOException e) { - } - } - } else { - try { - imageFilePath = Utilities.getPath(imageUri); - } catch (Exception e) { - FileLog.e("tmessages", e); - } - } + Bitmap bitmap = Utilities.getBitmap(imageUri); + processSendingPhoto(bitmap); } else if (requestCode == 2) { String videoPath = null; if (data != null) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index 65a32b3a3..c05466ffc 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -12,6 +12,7 @@ import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.database.Cursor; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; @@ -52,25 +53,14 @@ public class LaunchActivity extends PausableActivity { if (parcelable == null) { return; } - String path = null; + Bitmap bitmap = null; if (parcelable instanceof Uri) { - path = Utilities.getPath((Uri)parcelable); + bitmap = Utilities.getBitmap((Uri) parcelable); } else { - path = intent.getParcelableExtra(Intent.EXTRA_STREAM).toString(); - if (path.startsWith("content:")) { - Cursor cursor = getContentResolver().query(Uri.parse(path), new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); - if (cursor != null) { - cursor.moveToFirst(); - path = cursor.getString(0); - cursor.close(); - } - } + } - if (path != null) { - if (path.startsWith("file:")) { - path = path.replace("file://", ""); - } - NotificationCenter.Instance.addToMemCache(533, path); + if (bitmap != null) { + NotificationCenter.Instance.addToMemCache(536, bitmap); } } else if (intent.getType().startsWith("video/")) { Parcelable parcelable = intent.getParcelableExtra(Intent.EXTRA_STREAM);