1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

Fix error when choosing image from Documents manager

Cannot rely on Uri.getPath to return absolute path to create File object. Instead get the inputstream and create Bitmap first.
This commit is contained in:
Ammar Githam 2020-08-19 00:15:13 +09:00
parent 4a73aa1179
commit f9ef697111
3 changed files with 47 additions and 47 deletions

View File

@ -1,7 +1,6 @@
package awais.instagrabber.asyncs; package awais.instagrabber.asyncs;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
@ -10,8 +9,6 @@ import org.json.JSONObject;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -44,9 +41,7 @@ public class ImageUploader extends AsyncTask<ImageUploadOptions, Void, ImageUplo
ByteArrayOutputStream baos = null; ByteArrayOutputStream baos = null;
try { try {
final ImageUploadOptions options = imageUploadOptions[0]; final ImageUploadOptions options = imageUploadOptions[0];
final File file = options.getFile(); final Bitmap bitmap = options.getBitmap();
inputStream = new FileInputStream(file);
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
baos = new ByteArrayOutputStream(); baos = new ByteArrayOutputStream();
final boolean compressResult = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); final boolean compressResult = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
if (!compressResult) { if (!compressResult) {

View File

@ -2,6 +2,8 @@ package awais.instagrabber.fragments.directmessages;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
@ -27,6 +29,9 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.ArrayList; import java.util.ArrayList;
@ -280,35 +285,35 @@ public class DirectMessageThreadFragment extends Fragment {
} }
private void sendImage(final Uri imageUri) { private void sendImage(final Uri imageUri) {
final String path = imageUri.getPath(); try(InputStream inputStream = requireContext().getContentResolver().openInputStream(imageUri)) {
if (path == null) { final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Log.e(TAG, "uri path is null!"); // Upload Image
return; final ImageUploader imageUploader = new ImageUploader();
} imageUploader.setOnTaskCompleteListener(response -> {
final File file = new File(path); if (response == null || response.getResponseCode() != HttpURLConnection.HTTP_OK) {
// Upload Image Toast.makeText(requireContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
final ImageUploader imageUploader = new ImageUploader(); if (response != null && response.getResponse() != null) {
imageUploader.setOnTaskCompleteListener(response -> { Log.e(TAG, response.getResponse().toString());
if (response == null || response.getResponseCode() != HttpURLConnection.HTTP_OK) { }
Toast.makeText(requireContext(), R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); return;
if (response != null && response.getResponse() != null) {
Log.e(TAG, response.getResponse().toString());
} }
return; final JSONObject responseJson = response.getResponse();
} try {
final JSONObject responseJson = response.getResponse(); final String uploadId = responseJson.getString("upload_id");
try { // Broadcast
final String uploadId = responseJson.getString("upload_id"); final DirectThreadBroadcaster.ImageBroadcastOptions options = new DirectThreadBroadcaster.ImageBroadcastOptions(true, uploadId);
// Broadcast hasSentSomething = true;
final DirectThreadBroadcaster.ImageBroadcastOptions options = new DirectThreadBroadcaster.ImageBroadcastOptions(true, uploadId); broadcast(options, onBroadcastCompleteListener -> new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR));
hasSentSomething = true; } catch (JSONException e) {
broadcast(options, onBroadcastCompleteListener -> new DirectMessageInboxThreadFetcher(threadId, UserInboxDirection.OLDER, null, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)); Log.e(TAG, "Error parsing json response", e);
} catch (JSONException e) { }
Log.e(TAG, "Error parsing json response", e); });
} final ImageUploadOptions options = ImageUploadOptions.builder(bitmap).build();
}); imageUploader.execute(options);
final ImageUploadOptions options = ImageUploadOptions.builder(file).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) { private void broadcast(final DirectThreadBroadcaster.BroadcastOptions broadcastOptions, final DirectThreadBroadcaster.OnBroadcastCompleteListener listener) {

View File

@ -1,23 +1,23 @@
package awais.instagrabber.models; package awais.instagrabber.models;
import java.io.File; import android.graphics.Bitmap;
public class ImageUploadOptions { public class ImageUploadOptions {
private final File file; private final Bitmap bitmap;
private boolean isSidecar; private boolean isSidecar;
private String waterfallId; private String waterfallId;
public static class Builder { public static class Builder {
private File file; private Bitmap bitmap;
private boolean isSidecar; private boolean isSidecar;
private String waterfallId; private String waterfallId;
public Builder(final File file) { public Builder(final Bitmap bitmap) {
this.file = file; this.bitmap = bitmap;
} }
public Builder setFile(final File file) { public Builder setBitmap(final Bitmap bitmap) {
this.file = file; this.bitmap = bitmap;
return this; return this;
} }
@ -32,24 +32,24 @@ public class ImageUploadOptions {
} }
public ImageUploadOptions build() { 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); return new Builder(file);
} }
private ImageUploadOptions(final File file, private ImageUploadOptions(final Bitmap bitmap,
final boolean isSidecar, final boolean isSidecar,
final String waterfallId) { final String waterfallId) {
this.file = file; this.bitmap = bitmap;
this.isSidecar = isSidecar; this.isSidecar = isSidecar;
this.waterfallId = waterfallId; this.waterfallId = waterfallId;
} }
public File getFile() { public Bitmap getBitmap() {
return file; return bitmap;
} }
public boolean isSidecar() { public boolean isSidecar() {