Skip to content

Commit

Permalink
Merge pull request #20744 from wordpress-mobile/issue/20279
Browse files Browse the repository at this point in the history
Fixes: ActivityNotFoundException for camera
  • Loading branch information
zwarm authored May 2, 2024
2 parents f51cb45 + 02b6100 commit f079c77
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
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?) {
this@MediaPickerActivity.mediaCapturePath = 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?) {
this@EditPostActivity.mediaCapturePath = 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 @@ -2086,6 +2086,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

0 comments on commit f079c77

Please sign in to comment.