7

I am trying to get an image directly from the Google Photos app. I wish to bypass the app chooser usually started when using Intent.ACTION_GET_CONTENT and instead go directly to Google Photos if the user has it installed and allow the user to select an image which would then be returned to my app in an ActivityResult.

I have attempted to do this in the following manner:

if (callingActivity != null && isGooglePhotosInstalled(callingActivity)) {
    Intent intent = callingActivity.getPackageManager().getLaunchIntentForPackage(GOOGLE_PHOTOS_PACKAGE_NAME);
    intent.setAction(Intent.ACTION_GET_CONTENT); // ALSO TRIED Intent.ACTION_PICK
    intent.setType("image/*");
    try {
        callingActivity.startActivityForResult(intent, MediaPickerActivity.REQUEST_PHOTO_FROM_GOOGLE_PHOTOS);
    } catch (ActivityNotFoundException e) {
        showErrorMsgDialog(callingActivity, "You don't have Google Photos installed! Download it from the play store today.");
        e.printStackTrace();
    }
}

To check if Google Photos is installed I am using:

private static final String GOOGLE_PHOTOS_PACKAGE_NAME = "com.google.android.apps.photos";
public static boolean isGooglePhotosInstalled(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {
        return packageManager.getPackageInfo(GOOGLE_PHOTOS_PACKAGE_NAME, PackageManager.GET_ACTIVITIES) != null;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

Using this method I am able to successfully open Google Photos in selection mode, but upon selecting an image the Google Photos app crashes.

How can I accomplish this?

EDIT: If it helps, I have created a sample app project on Github to help with troubleshooting. The stacktrace of the crash from Google Photos is the following:

09-01 21:30:25.081    1109-1109/? E/Binder﹕ Unbound type: dli
Searched binders:
com.google.android.apps.photos.home.HomeActivity ->
com.google.android.apps.photos.PhotosApplication ->
com.google.android.apps.photos.PhotosApplication
java.lang.IllegalStateException: Unbound type: dli
Searched binders:
com.google.android.apps.photos.home.HomeActivity ->
com.google.android.apps.photos.PhotosApplication ->
com.google.android.apps.photos.PhotosApplication
        at noy.a(PG:210)
        at noy.a(PG:485)
        at hmm.b_(PG:1061)
        at myf.a(PG:54)
        at myc.a(PG:36)
        at hlm.d(PG:242)
        at hll.b(PG:2232)
        at fns.onClick(PG:1375)
        at fnr.onClick(PG:1408)
        at android.view.View.performClick(View.java:4761)
        at android.view.View$PerformClick.run(View.java:19767)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Sample Project Found Here: https://github.com/michaelgarnerdev/GooglePhotosIntent

Michael Garner
  • 1,042
  • 1
  • 10
  • 19

2 Answers2

7

Try the following code:

public void launchGooglePhotosPicker(Activity callingActivity) {
    if (callingActivity != null && isGooglePhotosInstalled()) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setType("image/*");
        List<ResolveInfo> resolveInfoList = callingActivity.getPackageManager().queryIntentActivities(intent, 0);
        for (int i = 0; i < resolveInfoList.size(); i++) {
            if (resolveInfoList.get(i) != null) {
                String packageName = resolveInfoList.get(i).activityInfo.packageName;
                if (GOOGLE_PHOTOS_PACKAGE_NAME.equals(packageName)) {
                    intent.setComponent(new ComponentName(packageName, resolveInfoList.get(i).activityInfo.name));
                    callingActivity.startActivityForResult(intent, REQUEST_PHOTO_FROM_GOOGLE_PHOTOS);
                    return;
                }
            }
        }
    }
}
Michael Garner
  • 1,042
  • 1
  • 10
  • 19
Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • Thank you for your answer, but unfortunately I am still seeing the same crash from Google Photos and the image is not being returned. I have added a sample project on GitHub if you are interested in directly changing and testing code. I tried `ACTION_PICK` first, but changed it to `ACTION_GET_CONTENT` to see if it would make a difference and it didn't. :( – Michael Garner Sep 02 '15 at 15:05
  • Actually, does Google Photos return normally if you use Intent.createChooser? – Derek Fung Sep 02 '15 at 15:20
  • Yes I think so, but I don't want to create a chooser. I want to strictly limit input to being from GooglePhotos. – Michael Garner Sep 02 '15 at 15:21
  • I mean you should actually try that as a control experiment, it looks like not a problem with the code – Derek Fung Sep 02 '15 at 15:26
  • Are you suggesting passing the intent we built into `Intent.createChooser()` or scrapping the current intent and going with a straightforward Image chooser intent? – Michael Garner Sep 02 '15 at 15:44
  • If it's the latter, I know that works and there are plenty of examples of it working. If you're talking about passing the current intent into `Intent.createChooser()` then unfortunately that doesn't help either. It might just be a bug with GooglePhotos – Michael Garner Sep 02 '15 at 15:47
  • downloaded your git project and fixed it, check edit, you didn't incorporate all the changes I said – Derek Fung Sep 02 '15 at 16:01
  • Oopsies. Suggested an edit for your answer and accepted. Thank you! – Michael Garner Sep 02 '15 at 16:16
6

You could just use intent.setPackage():

Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
intent.setPackage(GOOGLE_PHOTOS_PACKAGE_NAME);
callingActivity.startActivityForResult(intent, REQUEST_PHOTO_FROM_GOOGLE_PHOTOS);
Mikhail
  • 71
  • 2