diff --git a/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java b/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java index f7d97cd1..e4cdaa6e 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java +++ b/app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java @@ -1,7 +1,6 @@ package awais.instagrabber.asyncs; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; @@ -10,8 +9,6 @@ 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; @@ -44,9 +41,7 @@ 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()); + try(InputStream inputStream = requireContext().getContentResolver().openInputStream(imageUri)) { + final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); + // 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; } - 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(file).build(); - imageUploader.execute(options); + 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(bitmap).build(); + imageUploader.execute(options); + } + catch (IOException e) { + Log.e(TAG, "Error opening file", e); + } } 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 30918914..94ce93d1 100644 --- a/app/src/main/java/awais/instagrabber/models/ImageUploadOptions.java +++ b/app/src/main/java/awais/instagrabber/models/ImageUploadOptions.java @@ -1,23 +1,23 @@ package awais.instagrabber.models; -import java.io.File; +import android.graphics.Bitmap; public class ImageUploadOptions { - private final File file; + private final Bitmap bitmap; private boolean isSidecar; private String waterfallId; public static class Builder { - private File file; + private Bitmap bitmap; private boolean isSidecar; private String waterfallId; - public Builder(final File file) { - this.file = file; + public Builder(final Bitmap bitmap) { + this.bitmap = bitmap; } - public Builder setFile(final File file) { - this.file = file; + public Builder setBitmap(final Bitmap bitmap) { + this.bitmap = bitmap; return this; } @@ -32,24 +32,24 @@ public class ImageUploadOptions { } public ImageUploadOptions build() { - return new ImageUploadOptions(file, isSidecar, waterfallId); + return new ImageUploadOptions(bitmap, isSidecar, waterfallId); } } - public static Builder builder(final File file) { + public static Builder builder(final Bitmap file) { return new Builder(file); } - private ImageUploadOptions(final File file, + private ImageUploadOptions(final Bitmap bitmap, final boolean isSidecar, final String waterfallId) { - this.file = file; + this.bitmap = bitmap; this.isSidecar = isSidecar; this.waterfallId = waterfallId; } - public File getFile() { - return file; + public Bitmap getBitmap() { + return bitmap; } public boolean isSidecar() {