diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 8ae284413..b301cf5f0 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -1,5 +1,6 @@ package protect.card_locker; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.res.ColorStateList; @@ -43,6 +44,7 @@ import androidx.appcompat.widget.Toolbar; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.Guideline; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.FileProvider; import androidx.core.graphics.BlendModeColorFilterCompat; import androidx.core.graphics.BlendModeCompat; import androidx.core.graphics.ColorUtils; @@ -53,6 +55,7 @@ import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import java.io.File; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.text.DateFormat; @@ -150,8 +153,45 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements @Override public void onLongPress(MotionEvent e) { - // Also switch on long-press for accessibility - setMainImage(true, true); + openCurrentMainImageInGallery(); + } + + private void openCurrentMainImageInGallery() { + ImageType wantedImageType = imageTypes.get(mainImageIndex); + + File file = null; + + switch (wantedImageType) { + case IMAGE_FRONT: + file = Utils.retrieveCardImageAsFile(this, loyaltyCardId, ImageLocationType.front); + break; + case IMAGE_BACK: + file = Utils.retrieveCardImageAsFile(this, loyaltyCardId, ImageLocationType.back); + break; + case BARCODE: + Toast.makeText(this, R.string.barcodeLongPressMessage, Toast.LENGTH_SHORT).show(); + return; + default: + // Empty default case for now to keep the spotBugsRelease job happy + } + + // Do nothing if there is no file + if (file == null) { + Toast.makeText(this, R.string.failedToRetrieveImageFile, Toast.LENGTH_SHORT).show(); + return; + } + + try { + Intent intent = new Intent(Intent.ACTION_VIEW) + .setDataAndType(FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file), "image/*") + .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + startActivity(intent); + } + catch (ActivityNotFoundException e) { + // Display a toast message if an image viewer is not installed on device + Toast.makeText(this, R.string.failedLaunchingPhotoPicker, Toast.LENGTH_SHORT).show(); + e.printStackTrace(); + } } @Override diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 892cce2ed..5db5f5214 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -345,6 +345,14 @@ public class Utils { saveCardImage(context, bitmap, getCardImageFileName(loyaltyCardId, type)); } + public static File retrieveCardImageAsFile(Context context, String fileName) { + return context.getFileStreamPath(fileName); + } + + public static File retrieveCardImageAsFile(Context context, int loyaltyCardId, ImageLocationType type) { + return retrieveCardImageAsFile(context, getCardImageFileName(loyaltyCardId, type)); + } + static public Bitmap retrieveCardImage(Context context, String fileName) { FileInputStream in; try { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 83772d46e..ae2cf1589 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -250,7 +250,9 @@ Sort Show info Hide info - Swipe or long press to switch images + Swipe to switch images, hold to open image in the gallery app + Failed to retrieve image file + Only images can be opened in the gallery app Name Most Recently Used Expiry diff --git a/app/src/main/res/xml/file_provider_paths.xml b/app/src/main/res/xml/file_provider_paths.xml index 3cd946b00..66e6be690 100644 --- a/app/src/main/res/xml/file_provider_paths.xml +++ b/app/src/main/res/xml/file_provider_paths.xml @@ -1,3 +1,4 @@ + \ No newline at end of file