implemented upload profile image/banner api

This commit is contained in:
Mariotaku Lee 2015-05-11 19:45:20 +08:00
parent d247f1921e
commit e23c00505c
2 changed files with 34 additions and 29 deletions

View File

@ -20,15 +20,14 @@
package org.mariotaku.twidere.api.twitter.api; package org.mariotaku.twidere.api.twitter.api;
import org.mariotaku.simplerestapi.http.BodyType; import org.mariotaku.simplerestapi.http.BodyType;
import org.mariotaku.simplerestapi.http.mime.FileTypedData;
import org.mariotaku.simplerestapi.method.GET; import org.mariotaku.simplerestapi.method.GET;
import org.mariotaku.simplerestapi.method.POST; import org.mariotaku.simplerestapi.method.POST;
import org.mariotaku.simplerestapi.param.Body; import org.mariotaku.simplerestapi.param.Body;
import org.mariotaku.simplerestapi.param.Form; import org.mariotaku.simplerestapi.param.Form;
import org.mariotaku.simplerestapi.param.Part;
import org.mariotaku.simplerestapi.param.Query; import org.mariotaku.simplerestapi.param.Query;
import org.mariotaku.twidere.api.twitter.TwitterException;
import java.io.File;
import java.io.InputStream;
import org.mariotaku.twidere.api.twitter.model.AccountSettings; import org.mariotaku.twidere.api.twitter.model.AccountSettings;
import org.mariotaku.twidere.api.twitter.model.Category; import org.mariotaku.twidere.api.twitter.model.Category;
import org.mariotaku.twidere.api.twitter.model.IDs; 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.ProfileUpdate;
import org.mariotaku.twidere.api.twitter.model.ResponseList; import org.mariotaku.twidere.api.twitter.model.ResponseList;
import org.mariotaku.twidere.api.twitter.model.SettingsUpdate; import org.mariotaku.twidere.api.twitter.model.SettingsUpdate;
import org.mariotaku.twidere.api.twitter.TwitterException;
import org.mariotaku.twidere.api.twitter.model.User; import org.mariotaku.twidere.api.twitter.model.User;
@SuppressWarnings("RedundantThrows") @SuppressWarnings("RedundantThrows")
@ -122,23 +120,28 @@ public interface UsersResources {
@Body(BodyType.FORM) @Body(BodyType.FORM)
User updateProfile(@Form ProfileUpdate profileUpdate) throws TwitterException; 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; @POST("/account/update_profile_banner.json")
@Body(BodyType.MULTIPART)
void updateProfileBannerImage(File banner, int width, int height, int offsetLeft, int offsetTop) 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; 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) @POST("/account/update_profile_image.json")
throws TwitterException; @Body(BodyType.MULTIPART)
User updateProfileImage(@Part("image") FileTypedData data) throws TwitterException;
User updateProfileImage(File image) throws TwitterException;
User updateProfileImage(InputStream image) throws TwitterException;
@GET("/account/verify_credentials.json") @GET("/account/verify_credentials.json")
User verifyCredentials() throws TwitterException; User verifyCredentials() throws TwitterException;

View File

@ -25,7 +25,15 @@ import android.support.annotation.NonNull;
import android.support.v4.util.LongSparseArray; import android.support.v4.util.LongSparseArray;
import android.util.Log; import android.util.Log;
import org.mariotaku.simplerestapi.http.mime.FileTypedData;
import org.mariotaku.twidere.Constants; 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.ListResponse;
import org.mariotaku.twidere.model.SingleResponse; import org.mariotaku.twidere.model.SingleResponse;
import org.mariotaku.twidere.provider.TwidereDataStore.Notifications; import org.mariotaku.twidere.provider.TwidereDataStore.Notifications;
@ -38,14 +46,6 @@ import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Set; 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 class TwitterWrapper implements Constants {
public static int clearNotification(final Context context, final int notificationType, final long accountId) { 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, public static void updateProfileBannerImage(final Context context, final Twitter twitter,
final Uri imageUri, final boolean deleteImage) final Uri imageUri, final boolean deleteImage)
throws FileNotFoundException, TwitterException { throws FileNotFoundException, TwitterException {
InputStream is; InputStream is = null;
try { try {
is = context.getContentResolver().openInputStream(imageUri); is = context.getContentResolver().openInputStream(imageUri);
twitter.updateProfileBannerImage(is); twitter.updateProfileBannerImage(new FileTypedData(is, "image", -1, null));
} finally { } finally {
Utils.closeSilently(is);
if (deleteImage && "file".equals(imageUri.getScheme())) { if (deleteImage && "file".equals(imageUri.getScheme())) {
final File file = new File(imageUri.getPath()); final File file = new File(imageUri.getPath());
if (!file.delete()) { if (!file.delete()) {
@ -187,11 +188,12 @@ public class TwitterWrapper implements Constants {
public static User updateProfileImage(final Context context, final Twitter twitter, public static User updateProfileImage(final Context context, final Twitter twitter,
final Uri imageUri, final boolean deleteImage) final Uri imageUri, final boolean deleteImage)
throws FileNotFoundException, TwitterException { throws FileNotFoundException, TwitterException {
InputStream is; InputStream is = null;
try { try {
is = context.getContentResolver().openInputStream(imageUri); is = context.getContentResolver().openInputStream(imageUri);
return twitter.updateProfileImage(is); return twitter.updateProfileImage(new FileTypedData(is, "image", -1, null));
} finally { } finally {
Utils.closeSilently(is);
if (deleteImage && "file".equals(imageUri.getScheme())) { if (deleteImage && "file".equals(imageUri.getScheme())) {
final File file = new File(imageUri.getPath()); final File file = new File(imageUri.getPath());
if (!file.delete()) { if (!file.delete()) {