Upgraded image selection.

This commit is contained in:
Pierre 2018-05-02 18:46:58 +02:00
parent 2433b388b3
commit 2e932d640b
2 changed files with 65 additions and 30 deletions

View File

@ -5,7 +5,6 @@ import android.app.Fragment;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -32,16 +31,14 @@ import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
import org.communiquons.android.comunic.client.data.models.UserInfo; 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.runnables.ConversationRefreshRunnable;
import org.communiquons.android.comunic.client.data.utils.AccountUtils; 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.activities.MainActivity;
import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter; import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter;
import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener; import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener;
import org.communiquons.android.comunic.client.ui.utils.BitmapUtils; import org.communiquons.android.comunic.client.ui.utils.BitmapUtils;
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
import org.communiquons.android.comunic.client.ui.views.ScrollListView; import org.communiquons.android.comunic.client.ui.views.ScrollListView;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
@ -501,35 +498,15 @@ public class ConversationFragment extends Fragment
if(resultCode == RESULT_OK){ if(resultCode == RESULT_OK){
try { try {
Uri imageUri = data.getData(); //Get new message bitmap
InputStream imageStream = getActivity().getContentResolver() new_message_bitmap = BitmapUtils.IntentResultToBitmap(getActivity(), data);
.openInputStream(imageUri);
//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);
//Check for errors
if(new_message_bitmap == null){ if(new_message_bitmap == null){
Log.e(TAG, "Could not open temporary file!"); remove_picked_image();
return; return;
} }
//Schedule file deletion
tempFile.deleteOnExit();
//Append image //Append image
pick_image_button.setImageBitmap(new_message_bitmap); pick_image_button.setImageBitmap(new_message_bitmap);
@ -553,8 +530,8 @@ public class ConversationFragment extends Fragment
//Reset image button //Reset image button
pick_image_button.setImageBitmap(null); pick_image_button.setImageBitmap(null);
pick_image_button.setImageDrawable(getActivity().getResources(). pick_image_button.setImageDrawable(UiUtils.getDrawable(getActivity(),
getDrawable(android.R.drawable.ic_menu_gallery, getActivity().getTheme())); android.R.drawable.ic_menu_gallery));
} }
/** /**

View File

@ -1,12 +1,21 @@
package org.communiquons.android.comunic.client.ui.utils; package org.communiquons.android.comunic.client.ui.utils;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Base64; import android.util.Base64;
import android.util.Log;
import org.communiquons.android.comunic.client.data.utils.FilesUtils;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
/** /**
* Bitmap utilities * Bitmap utilities
@ -17,6 +26,11 @@ import java.io.File;
public class BitmapUtils { public class BitmapUtils {
/**
* Debug tag
*/
private static final String TAG = "BitmapUtils";
/** /**
* Convert a Bitmap into a base64-encoded string * Convert a Bitmap into a base64-encoded string
* *
@ -112,4 +126,48 @@ public class BitmapUtils {
return inSampleSize; return inSampleSize;
} }
/**
* Turn an intent data into Bitmap object
*
* @param context The context of the activity
* @param data Intent data
* @return Generated bitmap
* @throws FileNotFoundException In case of failure during the process
*/
@Nullable
public static Bitmap IntentResultToBitmap(Context context, Intent data)
throws FileNotFoundException {
Uri imageUri = data.getData();
InputStream imageStream = context.getContentResolver()
.openInputStream(imageUri);
//Create a temporary file
File tempFile = FilesUtils.getTempFile(context);
if(tempFile == null){
Log.e(TAG, "Could not create temporary file to store intent image!");
return null;
}
//Intend to transfer file
if(!FilesUtils.InputStreamToFile(imageStream, tempFile)){
Log.e(TAG, "Could not transfer the content of the image to the file !");
return null;
}
//Load bitmap
Bitmap bitmap = BitmapUtils.openResized(tempFile, 1198, 1198);
if(bitmap == null){
Log.e(TAG, "Could not open temporary file!");
return null;
}
//Schedule file deletion
tempFile.deleteOnExit();
return bitmap;
}
} }