From eabbabb2b22e6cf97a226905276cfa79ad3887ef Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 7 Apr 2021 17:03:07 +0800 Subject: Fix IllegalArgumentException when downloading media. --- .../infinityforreddit/activities/LinkResolverActivity.java | 6 ++++-- .../infinityforreddit/activities/ViewVideoActivity.java | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java index 10de74cc..7f834f48 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java @@ -14,6 +14,8 @@ import androidx.browser.customtabs.CustomTabColorSchemeParams; import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsService; +import org.apache.commons.io.FilenameUtils; + import java.util.ArrayList; import java.util.List; @@ -88,7 +90,7 @@ public class LinkResolverActivity extends AppCompatActivity { if (path.endsWith("jpg") || path.endsWith("png")) { Intent intent = new Intent(this, ViewImageOrGifActivity.class); String url = uri.toString(); - String fileName = url.substring(url.lastIndexOf('/') + 1); + String fileName = FilenameUtils.getName(path); intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, url); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, fileName); intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, fileName); @@ -96,7 +98,7 @@ public class LinkResolverActivity extends AppCompatActivity { } else if (path.endsWith("gif")) { Intent intent = new Intent(this, ViewImageOrGifActivity.class); String url = uri.toString(); - String fileName = url.substring(url.lastIndexOf('/') + 1); + String fileName = FilenameUtils.getName(path); intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, url); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, fileName); intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, fileName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java index 094657b9..61a12366 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -48,6 +48,8 @@ import com.google.android.material.snackbar.Snackbar; import com.thefuntasty.hauler.DragDirection; import com.thefuntasty.hauler.HaulerView; +import org.apache.commons.io.FilenameUtils; + import java.util.List; import javax.inject.Inject; @@ -289,7 +291,7 @@ public class ViewVideoActivity extends AppCompatActivity { } } else if (videoType == VIDEO_TYPE_DIRECT) { videoDownloadUrl = mVideoUri.toString(); - videoFileName = videoDownloadUrl.substring(videoDownloadUrl.lastIndexOf('/') + 1); + videoFileName = FilenameUtils.getName(videoDownloadUrl); // Produces DataSource instances through which media data is loaded. dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "Infinity")); // Prepare the player with the source. -- cgit v1.2.3