summaryrefslogtreecommitdiff
path: root/app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java')
-rw-r--r--app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java139
1 files changed, 139 insertions, 0 deletions
diff --git a/app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java b/app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java
new file mode 100644
index 0000000..58d1c14
--- /dev/null
+++ b/app/src/main/java/uk/co/blatech/blaupload/util/BlaImageLoader.java
@@ -0,0 +1,139 @@
+package uk.co.blatech.blaupload.util;
+
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Matrix;
+import android.opengl.GLES10;
+import android.os.AsyncTask;
+import android.util.Log;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+
+import javax.microedition.khronos.opengles.GL10;
+
+import uk.co.blatech.blaupload.R;
+import uk.co.blatech.blaupload.data.ImageItem;
+
+/**
+ * This class is used for loading images in the background
+ *
+ * Created by joe on 02/08/14.
+ */
+public class BlaImageLoader extends AsyncTask<String, Void, ImageItem> {
+
+ private Resources res;
+
+ //Get the resources object from the UI thread so we can load a placeholder image
+ public BlaImageLoader(Resources resources) {
+ this.res = resources;
+ }
+
+ /**
+ *
+ * @param args host, filename, id
+ * host - the hostname to download from, eg http://www.blaupload.co.uk
+ * filename - the filename with no path, eg image.jpg
+ * id - the ID of the image used by the UI to order the images
+ *
+ * TODO: Some generic way of adding the suffix used for thumbnails
+ *
+ * @return an ImageItem object which contains the ID, a Bitmap image object, and the filename
+ */
+ @Override
+ protected ImageItem doInBackground(String... args) {
+
+ Bitmap bmp = null;
+ String host = args[0];
+ String filename = args[1];
+ int id = Integer.parseInt(args[2]);
+ int[] maxTextureSize = new int[1];
+ maxTextureSize[0] = 4096;
+ GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);
+
+ //Add an extra .png on the end to match the current thumbnail filenames
+ String url = host + filename;
+
+ DefaultHttpClient client = new DefaultHttpClient();
+ HttpGet httpGet = new HttpGet(url);
+
+
+ try {
+ HttpResponse response = client.execute(httpGet);
+ StatusLine statusLine = response.getStatusLine();
+ int statusCode = statusLine.getStatusCode();
+ String[] parts = filename.split("\\.");
+ String extension = parts[parts.length-1];
+ //Create the Bitmap if the file exists
+ if (statusCode == 200 && isImageExtension(extension)) {
+ // If the thumbnail wasn't found, show a placeholder
+ HttpEntity entity = response.getEntity();
+ byte[] bytes = EntityUtils.toByteArray(entity);
+ try {
+ bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
+ } catch (Exception e) {
+ Log.e(BlaImageLoader.class.toString(), "Failed decoding " + filename);
+ e.printStackTrace();
+ bmp = BitmapFactory.decodeResource(res, R.drawable.x);
+ ImageItem image = new ImageItem(id, bmp, filename);
+ return image;
+ }
+ int bmpHeight = bmp.getHeight();
+ int bmpWidth = bmp.getWidth();
+ if (bmpWidth > maxTextureSize[0]) {
+
+ float ratio = (float)maxTextureSize[0]/(float)bmpWidth;
+ Matrix matrix = new Matrix();
+ bmpWidth = maxTextureSize[0];
+ bmpHeight = Math.round(bmpHeight * ratio);
+ matrix.postScale(ratio, ratio);
+ bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false);
+ } else if (bmpHeight > maxTextureSize[0]) {
+ float ratio = (float)maxTextureSize[0]/(float)bmpHeight;
+ bmpHeight = maxTextureSize[0];
+ bmpWidth = Math.round(bmpWidth * ratio);
+ Matrix matrix = new Matrix();
+ matrix.postScale(ratio, ratio);
+ bmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, false);
+ }
+ } else {
+ //If the file doesn't exist, use the placeholder instead
+ bmp = BitmapFactory.decodeResource(res, R.drawable.x);
+
+ }
+
+ //TODO: Error handling (Not sure how we get here)
+ } catch (ClientProtocolException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ ImageItem image = new ImageItem(id, bmp, filename);
+ return image;
+ }
+
+ @Override
+ protected void onPostExecute(ImageItem result) {
+ EventBus.getInstance().post(new ImageLoaderResultEvent(result));
+
+ }
+
+ private boolean isImageExtension(String extension) {
+ if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") ||
+ extension.equalsIgnoreCase("gif") || extension.equalsIgnoreCase("png")) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+}