Large image can be used in conversations

This commit is contained in:
Pierre 2018-04-14 15:21:03 +02:00
parent 5c0cedbc23
commit f9156f8e6a
4 changed files with 136 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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);
}