1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 22:57: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,12 +285,8 @@ 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!");
return;
}
final File file = new File(path);
// Upload Image // Upload Image
final ImageUploader imageUploader = new ImageUploader(); final ImageUploader imageUploader = new ImageUploader();
imageUploader.setOnTaskCompleteListener(response -> { imageUploader.setOnTaskCompleteListener(response -> {
@ -307,9 +308,13 @@ public class DirectMessageThreadFragment extends Fragment {
Log.e(TAG, "Error parsing json response", e); Log.e(TAG, "Error parsing json response", e);
} }
}); });
final ImageUploadOptions options = ImageUploadOptions.builder(file).build(); final ImageUploadOptions options = ImageUploadOptions.builder(bitmap).build();
imageUploader.execute(options); 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) {
final DirectThreadBroadcaster broadcaster = new DirectThreadBroadcaster(threadId); final DirectThreadBroadcaster broadcaster = new DirectThreadBroadcaster(threadId);

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() {