From e23c00505c45ca8d4d281fbc5e01d21880157ae5 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Mon, 11 May 2015 19:45:20 +0800 Subject: [PATCH] implemented upload profile image/banner api --- .../api/twitter/api/UsersResources.java | 37 ++++++++++--------- .../twidere/util/TwitterWrapper.java | 26 +++++++------ 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/UsersResources.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/UsersResources.java index a79db9130..708b97e77 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/UsersResources.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/UsersResources.java @@ -20,15 +20,14 @@ package org.mariotaku.twidere.api.twitter.api; import org.mariotaku.simplerestapi.http.BodyType; +import org.mariotaku.simplerestapi.http.mime.FileTypedData; import org.mariotaku.simplerestapi.method.GET; import org.mariotaku.simplerestapi.method.POST; import org.mariotaku.simplerestapi.param.Body; import org.mariotaku.simplerestapi.param.Form; +import org.mariotaku.simplerestapi.param.Part; import org.mariotaku.simplerestapi.param.Query; - -import java.io.File; -import java.io.InputStream; - +import org.mariotaku.twidere.api.twitter.TwitterException; import org.mariotaku.twidere.api.twitter.model.AccountSettings; import org.mariotaku.twidere.api.twitter.model.Category; import org.mariotaku.twidere.api.twitter.model.IDs; @@ -37,7 +36,6 @@ import org.mariotaku.twidere.api.twitter.model.Paging; import org.mariotaku.twidere.api.twitter.model.ProfileUpdate; import org.mariotaku.twidere.api.twitter.model.ResponseList; import org.mariotaku.twidere.api.twitter.model.SettingsUpdate; -import org.mariotaku.twidere.api.twitter.TwitterException; import org.mariotaku.twidere.api.twitter.model.User; @SuppressWarnings("RedundantThrows") @@ -122,23 +120,28 @@ public interface UsersResources { @Body(BodyType.FORM) User updateProfile(@Form ProfileUpdate profileUpdate) throws TwitterException; - User updateProfileBackgroundImage(File image, boolean tile) throws TwitterException; + @POST("/account/update_profile_background_image.json") + @Body(BodyType.MULTIPART) + User updateProfileBackgroundImage(@Part("image") FileTypedData data, @Part("tile") boolean tile) throws TwitterException; - User updateProfileBackgroundImage(InputStream image, boolean tile) throws TwitterException; + @POST("/account/update_profile_background_image.json") + @Body(BodyType.FORM) + User updateProfileBackgroundImage(@Form("media_id") long mediaId, @Part("tile") boolean tile) throws TwitterException; - void updateProfileBannerImage(File banner) throws TwitterException; - - void updateProfileBannerImage(File banner, int width, int height, int offsetLeft, int offsetTop) + @POST("/account/update_profile_banner.json") + @Body(BodyType.MULTIPART) + void updateProfileBannerImage(@Part("image") FileTypedData data, @Part("width") int width, + @Part("height") int height, @Part("offset_left") int offsetLeft, + @Part("offset_top") int offsetTop) throws TwitterException; - void updateProfileBannerImage(InputStream banner) throws TwitterException; + @POST("/account/update_profile_banner.json") + @Body(BodyType.MULTIPART) + void updateProfileBannerImage(@Part("image") FileTypedData data) throws TwitterException; - void updateProfileBannerImage(InputStream banner, int width, int height, int offsetLeft, int offsetTop) - throws TwitterException; - - User updateProfileImage(File image) throws TwitterException; - - User updateProfileImage(InputStream image) throws TwitterException; + @POST("/account/update_profile_image.json") + @Body(BodyType.MULTIPART) + User updateProfileImage(@Part("image") FileTypedData data) throws TwitterException; @GET("/account/verify_credentials.json") User verifyCredentials() throws TwitterException; diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterWrapper.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterWrapper.java index f0f98cd70..c020b2e3c 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterWrapper.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterWrapper.java @@ -25,7 +25,15 @@ import android.support.annotation.NonNull; import android.support.v4.util.LongSparseArray; import android.util.Log; +import org.mariotaku.simplerestapi.http.mime.FileTypedData; import org.mariotaku.twidere.Constants; +import org.mariotaku.twidere.api.twitter.Twitter; +import org.mariotaku.twidere.api.twitter.TwitterException; +import org.mariotaku.twidere.api.twitter.model.DirectMessage; +import org.mariotaku.twidere.api.twitter.model.Paging; +import org.mariotaku.twidere.api.twitter.model.ResponseList; +import org.mariotaku.twidere.api.twitter.model.Status; +import org.mariotaku.twidere.api.twitter.model.User; import org.mariotaku.twidere.model.ListResponse; import org.mariotaku.twidere.model.SingleResponse; import org.mariotaku.twidere.provider.TwidereDataStore.Notifications; @@ -38,14 +46,6 @@ import java.io.InputStream; import java.util.List; import java.util.Set; -import org.mariotaku.twidere.api.twitter.model.DirectMessage; -import org.mariotaku.twidere.api.twitter.model.Paging; -import org.mariotaku.twidere.api.twitter.model.ResponseList; -import org.mariotaku.twidere.api.twitter.model.Status; -import org.mariotaku.twidere.api.twitter.Twitter; -import org.mariotaku.twidere.api.twitter.TwitterException; -import org.mariotaku.twidere.api.twitter.model.User; - public class TwitterWrapper implements Constants { public static int clearNotification(final Context context, final int notificationType, final long accountId) { @@ -170,11 +170,12 @@ public class TwitterWrapper implements Constants { public static void updateProfileBannerImage(final Context context, final Twitter twitter, final Uri imageUri, final boolean deleteImage) throws FileNotFoundException, TwitterException { - InputStream is; + InputStream is = null; try { is = context.getContentResolver().openInputStream(imageUri); - twitter.updateProfileBannerImage(is); + twitter.updateProfileBannerImage(new FileTypedData(is, "image", -1, null)); } finally { + Utils.closeSilently(is); if (deleteImage && "file".equals(imageUri.getScheme())) { final File file = new File(imageUri.getPath()); if (!file.delete()) { @@ -187,11 +188,12 @@ public class TwitterWrapper implements Constants { public static User updateProfileImage(final Context context, final Twitter twitter, final Uri imageUri, final boolean deleteImage) throws FileNotFoundException, TwitterException { - InputStream is; + InputStream is = null; try { is = context.getContentResolver().openInputStream(imageUri); - return twitter.updateProfileImage(is); + return twitter.updateProfileImage(new FileTypedData(is, "image", -1, null)); } finally { + Utils.closeSilently(is); if (deleteImage && "file".equals(imageUri.getScheme())) { final File file = new File(imageUri.getPath()); if (!file.delete()) {