From 02b6100ef47f1d7a81c863c25b613dcef9d4a6db Mon Sep 17 00:00:00 2001 From: Pantelis Stampoulis Date: Wed, 1 May 2024 16:50:47 +0300 Subject: [PATCH] Fixes: ActivityNotFoundException for camera --- .../ui/media/MediaBrowserActivity.java | 4 ++++ .../ui/mediapicker/MediaPickerActivity.kt | 19 +++++++++++++++++- .../ui/photopicker/PhotoPickerActivity.java | 16 ++++++++++++++- .../android/ui/posts/EditPostActivity.kt | 20 ++++++++++++++++--- .../wordpress/android/util/WPMediaUtils.java | 10 +++++++++- WordPress/src/main/res/values/strings.xml | 1 + 6 files changed, 64 insertions(+), 6 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java index 46b0f4ab5c2a..2daf9e68f650 100755 --- a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java @@ -795,6 +795,10 @@ public void onMediaCapturePathReady(String mediaCapturePath) { mMediaCapturePath = mediaCapturePath; } + @Override public void onCameraError(String errorMessage) { + ToastUtils.showToast(this, errorMessage, LONG); + } + private void showMediaToastError(@StringRes int message, @Nullable String messageDetail) { if (isFinishing()) { return; diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerActivity.kt index 4f989fa1160a..4732b8ddac8e 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerActivity.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mediapicker/MediaPickerActivity.kt @@ -47,6 +47,7 @@ import org.wordpress.android.ui.posts.editor.ImageEditorTracker import org.wordpress.android.ui.utils.UiHelpers import org.wordpress.android.util.AppLog import org.wordpress.android.util.AppLog.T.MEDIA +import org.wordpress.android.util.ToastUtils import org.wordpress.android.util.WPMediaUtils import org.wordpress.android.util.extensions.getSerializableCompat import org.wordpress.android.util.extensions.getSerializableExtraCompat @@ -316,7 +317,23 @@ class MediaPickerActivity : LocaleAwareActivity(), MediaPickerListener { startActivityForResult(buildIntent(this, action.mediaPickerSetup, site, localPostId), PHOTO_PICKER) } OpenCameraForPhotos -> { - WPMediaUtils.launchCamera(this, BuildConfig.APPLICATION_ID) { mediaCapturePath = it } + WPMediaUtils.launchCamera( + this, + BuildConfig.APPLICATION_ID, + object : WPMediaUtils.LaunchCameraCallback { + override fun onMediaCapturePathReady(mediaCapturePath: String?) { + this@MediaPickerActivity.mediaCapturePath = mediaCapturePath + } + + override fun onCameraError(errorMessage: String?) { + ToastUtils.showToast( + this@MediaPickerActivity, + errorMessage, + ToastUtils.Duration.SHORT + ) + } + } + ) } } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java index ecb826c1a67f..db908316a905 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java @@ -34,6 +34,7 @@ import org.wordpress.android.util.ListUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.WPMediaUtils; +import org.wordpress.android.util.WPMediaUtils.LaunchCameraCallback; import java.io.File; import java.util.ArrayList; @@ -230,7 +231,20 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { private void launchCameraForImage() { WPMediaUtils.launchCamera(this, BuildConfig.APPLICATION_ID, - mediaCapturePath -> mMediaCapturePath = mediaCapturePath); + new LaunchCameraCallback() { + @Override + public void onMediaCapturePathReady(String mediaCapturePath) { + // Handle the path for the captured media + mMediaCapturePath = mediaCapturePath; + } + + @Override + public void onCameraError(String errorMessage) { + // Handle the error, e.g., display an error message to the user + ToastUtils.showToast(PhotoPickerActivity.this, errorMessage); + } + } + ); } private void launchPictureLibrary(boolean multiSelect) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt index 0a0f9691533a..24bb39d941be 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt @@ -2648,9 +2648,23 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm } private fun launchCamera() { - WPMediaUtils.launchCamera(this, BuildConfig.APPLICATION_ID) { mediaCapturePath -> - this.mediaCapturePath = mediaCapturePath - } + WPMediaUtils.launchCamera( + this, + BuildConfig.APPLICATION_ID, + object : WPMediaUtils.LaunchCameraCallback { + override fun onMediaCapturePathReady(mediaCapturePath: String?) { + this@EditPostActivity.mediaCapturePath = mediaCapturePath + } + + override fun onCameraError(errorMessage: String?) { + ToastUtils.showToast( + this@EditPostActivity, + errorMessage, + ToastUtils.Duration.SHORT + ) + } + } + ) } private fun setPostContentFromShareAction() { diff --git a/WordPress/src/main/java/org/wordpress/android/util/WPMediaUtils.java b/WordPress/src/main/java/org/wordpress/android/util/WPMediaUtils.java index c66d711b0762..24e47daeb759 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/WPMediaUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/util/WPMediaUtils.java @@ -48,6 +48,8 @@ public class WPMediaUtils { public interface LaunchCameraCallback { void onMediaCapturePathReady(String mediaCapturePath); + + void onCameraError(String errorMessage); } // 3000px is the utmost max resolution you can set in the picker but 2000px is the default max for optimized images. @@ -300,7 +302,13 @@ private static Intent prepareGalleryIntent(String title) { public static void launchCamera(Activity activity, String applicationId, LaunchCameraCallback callback) { Intent intent = prepareLaunchCamera(activity, applicationId, callback); if (intent != null) { - activity.startActivityForResult(intent, RequestCodes.TAKE_PHOTO); + // Check if there is an app that can handle the camera intent + if (intent.resolveActivity(activity.getPackageManager()) != null) { + activity.startActivityForResult(intent, RequestCodes.TAKE_PHOTO); + } else { + // Handle the case where no camera app is available + callback.onCameraError(activity.getString(R.string.error_no_camera_available)); + } } } diff --git a/WordPress/src/main/res/values/strings.xml b/WordPress/src/main/res/values/strings.xml index da8ea2487f2b..a102e246e78f 100644 --- a/WordPress/src/main/res/values/strings.xml +++ b/WordPress/src/main/res/values/strings.xml @@ -2084,6 +2084,7 @@ Unable to load this page right now. Check your network connection and try again. Couldn\'t update site title. Check your network connection and try again. + No camera app available. Could not find the post on the server