Download unprocessed gallery picture

Resolves #558
This commit is contained in:
scria1000 2021-11-30 19:42:45 +03:00
parent 3b365dfe5d
commit a70950baf9
3 changed files with 54 additions and 6 deletions

View File

@ -96,6 +96,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
private String subredditName; private String subredditName;
private boolean isDownloading = false; private boolean isDownloading = false;
private boolean isActionBarHidden = false; private boolean isActionBarHidden = false;
private boolean isFallback = false;
public ViewRedditGalleryImageOrGifFragment() { public ViewRedditGalleryImageOrGifFragment() {
// Required empty public constructor // Required empty public constructor
@ -162,6 +163,21 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
view.setQuickScaleEnabled(true); view.setQuickScaleEnabled(true);
view.resetScaleAndCenter(); view.resetScaleAndCenter();
} }
@Override
public void onImageLoadError(Exception e) {
e.printStackTrace();
// For issue #558
// Make sure it's not stuck in a loop if it comes to that
// Fallback url should be empty if it's not an album item
if (!isFallback && media.hasFallback()) {
imageView.cancel();
isFallback = true;
loadImage();
} else {
isFallback = false;
}
}
}); });
} }
} }
@ -238,7 +254,12 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
} }
private void loadImage() { private void loadImage() {
imageView.showImage(Uri.parse(media.url)); if(isFallback) {
imageView.showImage(Uri.parse(media.fallbackUrl));
}
else{
imageView.showImage(Uri.parse(media.url));
}
} }
@Override @Override
@ -295,7 +316,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
isDownloading = false; isDownloading = false;
Intent intent = new Intent(activity, DownloadMediaService.class); Intent intent = new Intent(activity, DownloadMediaService.class);
intent.putExtra(DownloadMediaService.EXTRA_URL, media.url); intent.putExtra(DownloadMediaService.EXTRA_URL, media.hasFallback() ? media.fallbackUrl : media.url); // Retrieve original instead of the one additionally compressed by reddit
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF: DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF: DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, media.fileName); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, media.fileName);
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
@ -303,8 +324,10 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
} }
//TODO: Find a way to share original image, Glide messes with the size and quality,
// compression should be up to the app being shared with (WhatsApp for example)
private void shareImage() { private void shareImage() {
glide.asBitmap().load(media.url).into(new CustomTarget<Bitmap>() { glide.asBitmap().load(media.hasFallback() ? media.fallbackUrl : media.url).into(new CustomTarget<Bitmap>() {
@Override @Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (activity.getExternalCacheDir() != null) { if (activity.getExternalCacheDir() != null) {
@ -420,7 +443,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
super.onResume(); super.onResume();
SubsamplingScaleImageView ssiv = imageView.getSSIV(); SubsamplingScaleImageView ssiv = imageView.getSSIV();
if (ssiv == null || !ssiv.hasImage()) { if (ssiv == null || !ssiv.hasImage()) {
imageView.showImage(Uri.parse(media.url)); loadImage();
} }
} }
@ -428,6 +451,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
imageView.cancel(); imageView.cancel();
isFallback = false;
SubsamplingScaleImageView subsamplingScaleImageView = imageView.getSSIV(); SubsamplingScaleImageView subsamplingScaleImageView = imageView.getSSIV();
if (subsamplingScaleImageView != null) { if (subsamplingScaleImageView != null) {
subsamplingScaleImageView.recycle(); subsamplingScaleImageView.recycle();

View File

@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.post;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.text.Html; import android.text.Html;
import android.text.TextUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -563,7 +564,17 @@ public class ParsePost {
previews.add(new Post.Preview(galleryItemUrl, singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY), previews.add(new Post.Preview(galleryItemUrl, singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY),
singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY))); singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY)));
} }
gallery.add(new Post.Gallery(mimeType, galleryItemUrl, subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1)));
Post.Gallery postGalleryItem = new Post.Gallery(mimeType, galleryItemUrl, "", subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1));
// For issue #558
// Construct a fallback image url
if(!TextUtils.isEmpty(galleryItemUrl) && !TextUtils.isEmpty(mimeType) && (mimeType.contains("jpg") || mimeType.contains("png"))) {
postGalleryItem.setFallbackUrl("https://i.redd.it/" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1));
postGalleryItem.setHasFallback(true);
}
gallery.add(postGalleryItem);
} }
if (!gallery.isEmpty()) { if (!gallery.isEmpty()) {

View File

@ -580,12 +580,15 @@ public class Post implements Parcelable {
public String mimeType; public String mimeType;
public String url; public String url;
public String fallbackUrl;
private boolean hasFallback;
public String fileName; public String fileName;
public int mediaType; public int mediaType;
public Gallery(String mimeType, String url, String fileName) { public Gallery(String mimeType, String url, String fallbackUrl, String fileName) {
this.mimeType = mimeType; this.mimeType = mimeType;
this.url = url; this.url = url;
this.fallbackUrl = fallbackUrl;
this.fileName = fileName; this.fileName = fileName;
if (mimeType.contains("gif")) { if (mimeType.contains("gif")) {
mediaType = TYPE_GIF; mediaType = TYPE_GIF;
@ -599,6 +602,8 @@ public class Post implements Parcelable {
protected Gallery(Parcel in) { protected Gallery(Parcel in) {
mimeType = in.readString(); mimeType = in.readString();
url = in.readString(); url = in.readString();
fallbackUrl = in.readString();
hasFallback = in.readByte() != 0;
fileName = in.readString(); fileName = in.readString();
mediaType = in.readInt(); mediaType = in.readInt();
} }
@ -624,9 +629,17 @@ public class Post implements Parcelable {
public void writeToParcel(Parcel parcel, int i) { public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(mimeType); parcel.writeString(mimeType);
parcel.writeString(url); parcel.writeString(url);
parcel.writeString(fallbackUrl);
parcel.writeByte((byte) (hasFallback ? 1 : 0));
parcel.writeString(fileName); parcel.writeString(fileName);
parcel.writeInt(mediaType); parcel.writeInt(mediaType);
} }
public void setFallbackUrl(String fallbackUrl) { this.fallbackUrl = fallbackUrl; }
public void setHasFallback(boolean hasFallback) { this.hasFallback = hasFallback; }
public boolean hasFallback() { return this.hasFallback; }
} }
public static class Preview implements Parcelable { public static class Preview implements Parcelable {