Browse Source

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.
legacy
Ammar Githam 4 years ago
parent
commit
f9ef697111
  1. 7
      app/src/main/java/awais/instagrabber/asyncs/ImageUploader.java
  2. 19
      app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java
  3. 26
      app/src/main/java/awais/instagrabber/models/ImageUploadOptions.java

7
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<ImageUploadOptions, Void, ImageUplo
ByteArrayOutputStream baos = null;
try {
final ImageUploadOptions options = imageUploadOptions[0];
final File file = options.getFile();
inputStream = new FileInputStream(file);
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
final Bitmap bitmap = options.getBitmap();
baos = new ByteArrayOutputStream();
final boolean compressResult = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
if (!compressResult) {

19
app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java

@ -2,6 +2,8 @@ package awais.instagrabber.fragments.directmessages;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
@ -27,6 +29,9 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
@ -280,12 +285,8 @@ public class DirectMessageThreadFragment extends Fragment {
}
private void sendImage(final Uri imageUri) {
final String path = imageUri.getPath();
if (path == null) {
Log.e(TAG, "uri path is null!");
return;
}
final File file = new File(path);
try(InputStream inputStream = requireContext().getContentResolver().openInputStream(imageUri)) {
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// Upload Image
final ImageUploader imageUploader = new ImageUploader();
imageUploader.setOnTaskCompleteListener(response -> {
@ -307,9 +308,13 @@ public class DirectMessageThreadFragment extends Fragment {
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);
}
catch (IOException e) {
Log.e(TAG, "Error opening file", e);
}
}
private void broadcast(final DirectThreadBroadcaster.BroadcastOptions broadcastOptions, final DirectThreadBroadcaster.OnBroadcastCompleteListener listener) {
final DirectThreadBroadcaster broadcaster = new DirectThreadBroadcaster(threadId);

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

Loading…
Cancel
Save