Fixed big image rendering issue

This commit is contained in:
Pierre 2018-01-02 08:52:22 +01:00
parent c2e599993b
commit 4f185600e2
4 changed files with 99 additions and 21 deletions

View File

@ -1,7 +1,10 @@
package org.communiquons.android.comunic.client.data.ImageLoad; package org.communiquons.android.comunic.client.data.ImageLoad;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.util.Log;
import org.communiquons.android.comunic.client.data.utils.BitmapUtils;
import org.communiquons.android.comunic.client.data.utils.Utilities;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -19,6 +22,11 @@ import java.net.URL;
class ImageDownloadRunnable implements Runnable { class ImageDownloadRunnable implements Runnable {
/**
* Debug tag
*/
private static final String TAG = "ImageDownloadRunnable";
private String url; private String url;
private File dest; private File dest;
@ -55,17 +63,34 @@ class ImageDownloadRunnable implements Runnable {
conn.connect(); conn.connect();
//Get image size
long img_size = Long.decode(conn.getHeaderField("Content-Length"));
//Get input stream //Get input stream
InputStream is = conn.getInputStream(); InputStream is = conn.getInputStream();
//Process image //Log action
Bitmap image = BitmapFactory.decodeStream(is); Log.v(TAG, "Downloading image (size: "+img_size+") at " + url);
image.compress(Bitmap.CompressFormat.PNG, 100, os);
os.close(); //Big images have to written byte per byte
Utilities.InputToOutputStream(is, os);
//Close streams and disconnect
is.close(); is.close();
os.close();
conn.disconnect(); conn.disconnect();
//Reduce image size
Bitmap bitmap = BitmapUtils.openResized(dest, 500, 500);
//Write the new bitmap to the file
os = new FileOutputStream(dest, false);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.close();
//Free memory
bitmap.recycle();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -22,6 +22,11 @@ import java.io.FileInputStream;
class ImageLoadRunnable implements Runnable { class ImageLoadRunnable implements Runnable {
/**
* Debug tag
*/
private static final String TAG = "ImageLoadRunnable";
/** /**
* An array map with all the pending images associated with their URLs * An array map with all the pending images associated with their URLs
*/ */
@ -136,6 +141,9 @@ class ImageLoadRunnable implements Runnable {
return; return;
} }
//Log action
Log.v(TAG, "Load image downloaded from " + url);
try { try {
//Load the image //Load the image
FileInputStream is = new FileInputStream(file); FileInputStream is = new FileInputStream(file);

View File

@ -1,11 +1,12 @@
package org.communiquons.android.comunic.client.data.utils; package org.communiquons.android.comunic.client.data.utils;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Base64; import android.util.Base64;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
/** /**
* Bitmap utilities * Bitmap utilities
@ -58,4 +59,57 @@ public class BitmapUtils {
//Scale and return bitmap //Scale and return bitmap
return Bitmap.createScaledBitmap(bitmap, newW, newH, true); return Bitmap.createScaledBitmap(bitmap, newW, newH, true);
} }
/**
* Open a resized format of an image to avoid OOM Fatal error
*
* @param file The file to open
* @param reqWidth The required width
* @param reqHeight The required height
*/
public static Bitmap openResized(@NonNull File file, int reqWidth, int reqHeight){
//Get the size of the image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
//Calculate the reduction ratio
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
//Decode Bitmap with new parametres
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}
/**
* Calculate the in sample size
*
* @param options The Options of the Bitmap Factory that already contains the with and the
* height of the image
* @param reqW The required width
* @param reqH The required Height
* @return The reduction ratio required for the image
*/
private static int calculateInSampleSize(BitmapFactory.Options options, int reqW, int reqH){
//Raw the width and the height of the image
int width = options.outWidth;
int height = options.outHeight;
int inSampleSize = 1;
//Check if the image has to be resized
if(height > reqH || width > reqW){
int halfHeight = height / 2;
int halfWidth = width / 2;
while((halfHeight / inSampleSize) >= reqH && (halfWidth / inSampleSize) >= reqW)
inSampleSize *= 2;
}
return inSampleSize;
}
} }

View File

@ -2,19 +2,15 @@ package org.communiquons.android.comunic.client.data.utils;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull;
import android.text.Html; import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.util.Patterns; import android.util.Patterns;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -176,25 +172,20 @@ public class Utilities {
* *
* @param is The Input stream * @param is The Input stream
* @param os The output stream * @param os The output stream
* @return The number of byte transfered
*/ */
public static int InputToOutputStream(InputStream is, OutputStream os){ public static void InputToOutputStream(InputStream is, OutputStream os){
int count = 0; int read = 0;
byte[] bytes = new byte[2048];
try { try {
int b = is.read(); while ((read = is.read(bytes)) != -1){
while (b != -1){ os.write(bytes, 0, read);
os.write(b);
count++;
b = is.read();
} }
} catch (IOException e){ } catch (IOException e){
e.printStackTrace(); e.printStackTrace();
} }
return count;
} }
/** /**