From 4a73aa1179388e341b59e27edfb11a0116b214d1 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 18 Aug 2020 22:22:16 +0900 Subject: [PATCH] allow uploading any type of image Tested all formats (except MNG) from https://www.w3.org/People/mimasa/test/imgformat/ --- .../instagrabber/asyncs/ImageUploader.java | 50 +++++++++------ .../DirectMessageThreadFragment.java | 63 +++++++++---------- .../models/ImageUploadOptions.java | 42 +++++-------- 3 files changed, 74 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java b/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java index 41454158..f7d97cd1 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java +++ b/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java @@ -1,11 +1,17 @@ package awais.instagrabber.asyncs; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; import org.json.JSONObject; +import java.io.BufferedOutputStream; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -35,13 +41,24 @@ public class ImageUploader extends AsyncTask headers = new HashMap<>(); final String uploadId = String.valueOf(new Date().getTime()); final long random = LOWER + new Random().nextLong() * (UPPER - LOWER + 1); final String name = String.format("%s_0_%s", uploadId, random); - final String contentLength = String.valueOf(options.getContentLength()); final String waterfallId = options.getWaterfallId() != null ? options.getWaterfallId() : UUID.randomUUID().toString(); headers.put("X-Entity-Type", "image/jpeg"); headers.put("Offset", "0"); @@ -58,20 +75,15 @@ public class ImageUploader extends AsyncTask { + if (response == null || response.getResponseCode() != HttpURLConnection.HTTP_OK) { + Toast.makeText(requireContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + if (response != null && response.getResponse() != null) { + Log.e(TAG, response.getResponse().toString()); + } return; } - final long contentLength = fileDescriptor.getStatSize(); - final InputStream inputStream = requireContext().getContentResolver().openInputStream(imageUri); - // Upload Image - final ImageUploader imageUploader = new ImageUploader(); - imageUploader.setOnTaskCompleteListener(response -> { - if (response == null || response.getResponseCode() != HttpURLConnection.HTTP_OK) { - Toast.makeText(requireContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - if (response != null && response.getResponse() != null) { - Log.e(TAG, response.getResponse().toString()); - } - return; - } - final JSONObject responseJson = response.getResponse(); - try { - final String uploadId = responseJson.getString("upload_id"); - // Broadcast - final DirectThreadBroadcaster.ImageBroadcastOptions options = new DirectThreadBroadcaster.ImageBroadcastOptions(true, uploadId); - hasSentSomething = true; - broadcast(options, onBroadcastCompleteListener -> new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)); - } catch (JSONException e) { - Log.e(TAG, "Error parsing json response", e); - } - }); - final ImageUploadOptions options = ImageUploadOptions.builder(inputStream, contentLength).build(); - imageUploader.execute(options); - } catch (FileNotFoundException e) { - Log.e(TAG, "Error opening InputStream", e); - } + final JSONObject responseJson = response.getResponse(); + try { + final String uploadId = responseJson.getString("upload_id"); + // Broadcast + final DirectThreadBroadcaster.ImageBroadcastOptions options = new DirectThreadBroadcaster.ImageBroadcastOptions(true, uploadId); + hasSentSomething = true; + broadcast(options, onBroadcastCompleteListener -> new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)); + } catch (JSONException e) { + Log.e(TAG, "Error parsing json response", e); + } + }); + final ImageUploadOptions options = ImageUploadOptions.builder(file).build(); + imageUploader.execute(options); } private void broadcast(final DirectThreadBroadcaster.BroadcastOptions broadcastOptions, final DirectThreadBroadcaster.OnBroadcastCompleteListener listener) { diff --git a/app/src/main/java/awais/instagrabber/models/ImageUploadOptions.java b/app/src/main/java/awais/instagrabber/models/ImageUploadOptions.java index c7006fc2..30918914 100644 --- a/app/src/main/java/awais/instagrabber/models/ImageUploadOptions.java +++ b/app/src/main/java/awais/instagrabber/models/ImageUploadOptions.java @@ -1,31 +1,23 @@ package awais.instagrabber.models; -import java.io.InputStream; +import java.io.File; public class ImageUploadOptions { - private InputStream inputStream; - private long contentLength; + private final File file; private boolean isSidecar; private String waterfallId; public static class Builder { - private InputStream inputStream; - private long contentLength; + private File file; private boolean isSidecar; private String waterfallId; - public Builder(final InputStream inputStream, final long contentLength) { - this.inputStream = inputStream; - this.contentLength = contentLength; + public Builder(final File file) { + this.file = file; } - public Builder setInputStream(final InputStream inputStream) { - this.inputStream = inputStream; - return this; - } - - public Builder setContentLength(final long contentLength) { - this.contentLength = contentLength; + public Builder setFile(final File file) { + this.file = file; return this; } @@ -40,30 +32,24 @@ public class ImageUploadOptions { } public ImageUploadOptions build() { - return new ImageUploadOptions(inputStream, contentLength, isSidecar, waterfallId); + return new ImageUploadOptions(file, isSidecar, waterfallId); } } - public static Builder builder(final InputStream inputStream, final long contentLength) { - return new Builder(inputStream, contentLength); + public static Builder builder(final File file) { + return new Builder(file); } - private ImageUploadOptions(final InputStream inputStream, - final long contentLength, + private ImageUploadOptions(final File file, final boolean isSidecar, final String waterfallId) { - this.inputStream = inputStream; - this.contentLength = contentLength; + this.file = file; this.isSidecar = isSidecar; this.waterfallId = waterfallId; } - public InputStream getInputStream() { - return inputStream; - } - - public long getContentLength() { - return contentLength; + public File getFile() { + return file; } public boolean isSidecar() {