mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-26 00:27:30 +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:
parent
4a73aa1179
commit
f9ef697111
@ -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) {
|
||||
|
@ -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,35 +285,35 @@ 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);
|
||||
// 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());
|
||||
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) {
|
||||
|
@ -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…
Reference in New Issue
Block a user