Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: ActivityNotFoundException for camera #20744

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
[email protected] = mediaCapturePath
}

override fun onCameraError(errorMessage: String?) {
ToastUtils.showToast(
this@MediaPickerActivity,
errorMessage,
ToastUtils.Duration.SHORT
)
}
}
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
[email protected] = mediaCapturePath
}

override fun onCameraError(errorMessage: String?) {
ToastUtils.showToast(
this@EditPostActivity,
errorMessage,
ToastUtils.Duration.SHORT
)
}
}
)
}

private fun setPostContentFromShareAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,7 @@
<string name="error_browser_no_network">Unable to load this page right now.</string>
<string name="error_network_connection">Check your network connection and try again.</string>
<string name="error_update_site_title_network">Couldn\'t update site title. Check your network connection and try again.</string>
<string name="error_no_camera_available">No camera app available.</string>

<!-- Post Error -->
<string name="error_unknown_post">Could not find the post on the server</string>
Expand Down
Loading