diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FilesUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FilesUtils.java new file mode 100644 index 0000000..895a027 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/FilesUtils.java @@ -0,0 +1,78 @@ +package org.communiquons.android.comunic.client.data.utils; + +import android.content.Context; +import android.support.annotation.Nullable; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Random; + +/** + * Files utilities + * + * @author Pierre HUBERT + * Created by pierre on 4/14/18. + */ + +public class FilesUtils { + + /** + * Temporary directory + */ + private final static String TEMP_DIRECTORY = "temp"; + + /** + * Create and return a temporary file + * + * @param context The context of the application + * @return Generated file / FALSE in case failure + */ + @Nullable + public static File getTempFile(Context context){ + + //Get cache directory + File cacheDir = context.getCacheDir(); + + File target; + do { + + //Generate target file + target = new File(cacheDir, + TEMP_DIRECTORY + File.pathSeparator + StringsUtils.random(10)); + + } while (target.exists()); + + //Try to create the file + try { + return target.createNewFile() ? target : null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + /** + * Read the content of an input stream into a file + * + * @param is Source input stream + * @param file Target file + * @return TRUE in case of sucess / FALSE else + */ + public static boolean InputStreamToFile(InputStream is, File file){ + + try { + FileOutputStream os = new FileOutputStream(file, false); + Utilities.InputToOutputStream(is, os); + os.close(); + + } catch (java.io.IOException e) { + e.printStackTrace(); + return false; + } + + return true; + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java index ec6f56f..8d6190d 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/StringsUtils.java @@ -1,5 +1,7 @@ package org.communiquons.android.comunic.client.data.utils; +import java.util.Random; + /** * Strings utilities * @@ -25,4 +27,24 @@ public class StringsUtils { return true; } + /** + * Generate a random string + * + * @param length The length of the string to generate + * @return Generated string + */ + public static String random(int length){ + + Random generator = new Random(); + StringBuilder randomStringBuilder = new StringBuilder(); + char tempChar; + for (int i = 0; i < length; i++){ + tempChar = (char) (generator.nextInt(96) + 32); + randomStringBuilder.append(tempChar); + } + + return randomStringBuilder.toString(); + + } + } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java index 97f68b9..d52da1f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java @@ -12,6 +12,7 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.ArrayMap; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -22,20 +23,22 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; -import org.communiquons.android.comunic.client.ui.activities.MainActivity; import org.communiquons.android.comunic.client.R; -import org.communiquons.android.comunic.client.data.utils.AccountUtils; +import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper; +import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper; -import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.data.models.ConversationMessage; -import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter; -import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper; -import org.communiquons.android.comunic.client.data.runnables.ConversationRefreshRunnable; import org.communiquons.android.comunic.client.data.models.ConversationsInfo; -import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; +import org.communiquons.android.comunic.client.data.models.UserInfo; +import org.communiquons.android.comunic.client.data.runnables.ConversationRefreshRunnable; +import org.communiquons.android.comunic.client.data.utils.AccountUtils; +import org.communiquons.android.comunic.client.data.utils.FilesUtils; +import org.communiquons.android.comunic.client.ui.activities.MainActivity; +import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter; import org.communiquons.android.comunic.client.ui.utils.BitmapUtils; +import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; @@ -455,7 +458,31 @@ public class ConversationFragment extends Fragment Uri imageUri = data.getData(); InputStream imageStream = getActivity().getContentResolver() .openInputStream(imageUri); - new_message_bitmap = BitmapFactory.decodeStream(imageStream); + + //Create a temporary file + File tempFile = FilesUtils.getTempFile(getActivity()); + + if(tempFile == null){ + Log.e(TAG, "Could not create temporary file to store conversation image!"); + return; + } + + //Intend to transfer file + if(!FilesUtils.InputStreamToFile(imageStream, tempFile)){ + Log.e(TAG, "Could not transfer the content of the image to the file !"); + return; + } + + //Load bitmap + new_message_bitmap = BitmapUtils.openResized(tempFile, 1198, 1198); + + if(new_message_bitmap == null){ + Log.e(TAG, "Could not open temporary file!"); + return; + } + + //Schedule file deletion + tempFile.deleteOnExit(); //Append image pick_image_button.setImageBitmap(new_message_bitmap); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/BitmapUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/BitmapUtils.java index 3ca5745..3313454 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/BitmapUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/BitmapUtils.java @@ -77,7 +77,7 @@ public class BitmapUtils { //Calculate the reduction ratio options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); - //Decode Bitmap with new parametres + //Decode Bitmap with new parameters options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(file.getAbsolutePath(), options); }