From 6e0e86d6b7c31927ff781b72247a3da76aeaa28d Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 5 Mar 2013 16:44:19 -0800 Subject: [PATCH] Some progress towards uploading pictures as multipart entities --- .../com/todoroo/astrid/data/RemoteModel.java | 69 ++++++++++++++----- .../astrid/actfm/CommentsFragment.java | 2 +- .../astrid/actfm/TagSettingsActivity.java | 2 +- .../astrid/notes/EditNoteActivity.java | 6 +- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/api/src/com/todoroo/astrid/data/RemoteModel.java b/api/src/com/todoroo/astrid/data/RemoteModel.java index 9fe469703..1fe72eea7 100644 --- a/api/src/com/todoroo/astrid/data/RemoteModel.java +++ b/api/src/com/todoroo/astrid/data/RemoteModel.java @@ -5,18 +5,25 @@ */ package com.todoroo.astrid.data; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + import org.json.JSONException; import org.json.JSONObject; import android.content.ContentValues; +import android.content.Context; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.text.TextUtils; import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.StringProperty; import com.todoroo.andlib.data.TodorooCursor; -import com.todoroo.andlib.utility.AndroidUtilities; +import com.todoroo.andlib.utility.DateUtilities; /** * A model that is synchronized to a remote server and has a remote id @@ -108,12 +115,49 @@ abstract public class RemoteModel extends AbstractModel { public static class PictureHelper { + public static final String PICTURES_DIRECTORY = "pictures"; //$NON-NLS-1$ + + @SuppressWarnings("nls") + public static JSONObject savePictureJson(Context context, Bitmap bitmap) { + try { + String name = DateUtilities.now() + ".jpg"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put("name", name); + jsonObject.put("type", "image/jpeg"); + + File dir = context.getExternalFilesDir(PICTURES_DIRECTORY); + if (dir != null) { + File file = new File(dir + File.separator + DateUtilities.now() + ".jpg"); + if (file.exists()) + return null; + + try { + FileOutputStream fos = new FileOutputStream(file); + bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); + fos.flush(); + fos.close(); + jsonObject.put("path", file.getAbsolutePath()); + } catch (FileNotFoundException e) { + // + } catch (IOException e) { + // + } + return jsonObject; + } else { + return null; + } + } catch (JSONException e) { + // + } + return null; + } + public static String getPictureUrl(String value, String size) { try { if (value == null) return null; JSONObject pictureJson = new JSONObject(value); - if (pictureJson.has("data")) // Unpushed encoded bitmap //$NON-NLS-1$ + if (pictureJson.has("path")) // Unpushed encoded bitmap //$NON-NLS-1$ return null; return pictureJson.optString(size); } catch (JSONException e) { @@ -126,11 +170,11 @@ abstract public class RemoteModel extends AbstractModel { try { if (value == null) return null; - if (value.contains("data")) { + if (value.contains("path")) { JSONObject pictureJson = new JSONObject(value); - if (pictureJson.has("data")) { - String data = pictureJson.getString("data"); - return AndroidUtilities.decodeBase64Bitmap(data); + if (pictureJson.has("path")) { + String path = pictureJson.getString("path"); + return BitmapFactory.decodeFile(path); } } return null; @@ -144,18 +188,5 @@ abstract public class RemoteModel extends AbstractModel { String value = cursor.get(pictureProperty); return getPictureUrl(value, size); } - - @SuppressWarnings("nls") - public static JSONObject uploadPictureJson(Bitmap bitmap) { - try { - JSONObject pictureJson = new JSONObject(); - pictureJson.put("name", "photo.jpg"); - pictureJson.put("type", "image/jpeg"); - pictureJson.put("data", AndroidUtilities.encodeBase64Bitmap(bitmap)); - return pictureJson; - } catch (JSONException e) { - return null; - } - } } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/CommentsFragment.java b/astrid/plugin-src/com/todoroo/astrid/actfm/CommentsFragment.java index 1833b0f1b..eb0693b01 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/CommentsFragment.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/CommentsFragment.java @@ -325,7 +325,7 @@ public abstract class CommentsFragment extends ListFragment { protected void addComment() { UserActivity update = createUpdate(); if (picture != null) { - JSONObject pictureJson = RemoteModel.PictureHelper.uploadPictureJson(picture); + JSONObject pictureJson = RemoteModel.PictureHelper.savePictureJson(getActivity(), picture); if (pictureJson != null) { update.setValue(UserActivity.PICTURE, pictureJson.toString()); } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java b/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java index 4a4672073..47de92de5 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java @@ -292,7 +292,7 @@ public class TagSettingsActivity extends FragmentActivity { tagData.setValue(TagData.TAG_DESCRIPTION, newDesc); if (setBitmap != null) { - JSONObject pictureJson = RemoteModel.PictureHelper.uploadPictureJson(setBitmap); + JSONObject pictureJson = RemoteModel.PictureHelper.savePictureJson(this, setBitmap); if (pictureJson != null) tagData.setValue(TagData.PICTURE, pictureJson.toString()); } diff --git a/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java b/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java index f0aa259ff..b87c017c5 100644 --- a/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/notes/EditNoteActivity.java @@ -12,6 +12,8 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; +import org.json.JSONObject; + import android.app.Activity; import android.content.Intent; import android.content.res.Resources; @@ -448,7 +450,9 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene userActivity.setValue(UserActivity.TARGET_NAME, task.getValue(Task.TITLE)); userActivity.setValue(UserActivity.CREATED_AT, DateUtilities.now()); if (usePicture && pendingCommentPicture != null) { - userActivity.setValue(UserActivity.PICTURE, RemoteModel.PictureHelper.uploadPictureJson(pendingCommentPicture).toString()); + JSONObject pictureJson = RemoteModel.PictureHelper.savePictureJson(activity, pendingCommentPicture); + if (pictureJson != null) + userActivity.setValue(UserActivity.PICTURE, pictureJson.toString()); } userActivityDao.createNew(userActivity);