From a01fb65e46f7f05dae5a12a4f1b500255b48c84a Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Fri, 8 May 2015 16:08:46 +0800 Subject: [PATCH] handles image paste event in compose activity --- .../activity/support/ImagePickerActivity.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/twidere/src/main/java/org/mariotaku/twidere/activity/support/ImagePickerActivity.java b/twidere/src/main/java/org/mariotaku/twidere/activity/support/ImagePickerActivity.java index 2451e8df6..a30bc53f0 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/activity/support/ImagePickerActivity.java +++ b/twidere/src/main/java/org/mariotaku/twidere/activity/support/ImagePickerActivity.java @@ -22,18 +22,24 @@ import android.webkit.MimeTypeMap; import com.nostra13.universalimageloader.utils.IoUtils; +import org.mariotaku.simplerestapi.http.ContentType; +import org.mariotaku.simplerestapi.http.RestHttpClient; +import org.mariotaku.simplerestapi.http.RestRequest; +import org.mariotaku.simplerestapi.http.RestResponse; +import org.mariotaku.simplerestapi.http.mime.TypedData; +import org.mariotaku.simplerestapi.method.GET; import org.mariotaku.twidere.R; import org.mariotaku.twidere.fragment.ProgressDialogFragment; import org.mariotaku.twidere.fragment.support.BaseSupportDialogFragment; import org.mariotaku.twidere.model.SingleResponse; import org.mariotaku.twidere.util.ThemeUtils; +import org.mariotaku.twidere.util.TwitterAPIUtils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.URL; import static android.os.Environment.getExternalStorageState; @@ -188,15 +194,28 @@ public class ImagePickerActivity extends ThemedFragmentActivity { try { final File cacheDir = mActivity.getCacheDir(); final Uri uri = this.mUri; + final String mimeType; if (SCHEME_HTTP.equals(uri.getScheme()) || SCHEME_HTTPS.equals(uri.getScheme())) { - is = new URL(uri.toString()).openStream(); + final RestHttpClient client = TwitterAPIUtils.getDefaultHttpClient(mActivity); + final RestRequest.Builder builder = new RestRequest.Builder(); + builder.method(GET.METHOD); + builder.url(uri.toString()); + final RestResponse response = client.execute(builder.build()); + if (response.isSuccessful()) { + final TypedData body = response.getBody(); + is = body.stream(); + final ContentType contentType = body.contentType(); + mimeType = contentType != null ? contentType.getContentType() : "image/*"; + } else { + throw new IOException("Unable to get " + uri); + } } else { is = cr.openInputStream(uri); + final BitmapFactory.Options opts = new BitmapFactory.Options(); + opts.inJustDecodeBounds = true; + BitmapFactory.decodeStream(cr.openInputStream(uri), null, opts); + mimeType = opts.outMimeType; } - final BitmapFactory.Options opts = new BitmapFactory.Options(); - opts.inJustDecodeBounds = true; - BitmapFactory.decodeStream(cr.openInputStream(uri), null, opts); - final String mimeType = opts.outMimeType; final String suffix = mimeType != null ? "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) : null; final File outFile = File.createTempFile("temp_image_", suffix, cacheDir);