如何仅从Google相册应用程序中选择图像?Android Google相册意图

7
我正在尝试直接从Google相册应用程序获取图像。我希望绕过使用Intent.ACTION_GET_CONTENT时通常启动的应用程序选择器,而是直接进入Google相册,如果用户已经安装了它,允许用户选择图像,然后将其作为ActivityResult返回到我的应用程序中。
我尝试以以下方式实现这个目标:
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();
    }
}

我正在使用以下方法检查是否安装了Google相册:
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;
    }
}

使用这种方法,我能够成功地在选择模式下打开Google照片,但在选择图像后,Google照片应用程序会崩溃。
我该怎么做呢?
编辑: 如果有帮助的话,我在Github上创建了一个示例应用程序项目来帮助进行故障排除。从Google照片中崩溃的堆栈跟踪如下:
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)

在此找到示例项目: https://github.com/michaelgarnerdev/GooglePhotosIntent
2个回答

7

尝试以下代码:

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;
                }
            }
        }
    }
}

谢谢您的回答,但不幸的是我仍然看到来自Google照片的相同崩溃,并且图像没有被返回。如果您有兴趣直接更改和测试代码,我已经在GitHub上添加了一个示例项目。我首先尝试了ACTION_PICK,但将其更改为ACTION_GET_CONTENT以查看是否会有所不同,但结果并没有改变。:( - Michael Garner
如果使用Intent.createChooser,Google Photos是否会正常返回? - Derek Fung
是的,我想是这样,但我不想创建一个选择器。我想严格限制输入来自GooglePhotos。 - Michael Garner
我的意思是你应该尝试作为控制实验,看起来不是代码的问题。 - Derek Fung
如果是后者,我知道它可以工作,并且有很多例子证明它可以工作。如果你说的是将当前意图传递到Intent.createChooser()中,那么不幸的是这也没有帮助。这可能只是GooglePhotos的一个错误。 - Michael Garner
显示剩余3条评论

6
您可以使用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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接