2015-09-01 107 views
4

我想從Google相冊應用中直接獲取圖片。我希望繞過通常在使用Intent.ACTION_GET_CONTENT時啓動的應用選擇器,如果用戶已安裝它並直接轉到Google相冊,並允許用戶選擇一個圖像,然後將該圖像返回到我的應用程序的一個ActivityResult。如何從Google照片應用中選擇圖片? Android Google相冊意圖

我試圖做到這一點通過以下方式:

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

要檢查是否安裝了谷歌的照片,我使用:

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

使用這種方法我能要在選擇模式下成功打開Goog​​le照片,但選擇圖片後Google相冊應用會崩潰。

我該如何做到這一點?

編輯: 如果有幫助,我已經在Github上創建了一個示例應用程序項目來幫助解決問題。從谷歌的照片崩潰的堆棧跟蹤如下:

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

回答

5

試試下面的代碼:

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

謝謝你的回答,但不幸的是我是s直到看到來自Google照片的相同的崩潰,並且圖像沒有被返回。如果您有興趣直接更改和測試代碼,我在GitHub上添加了一個示例項目。我首先嚐試了'ACTION_PICK',但將其改爲'ACTION_GET_CONTENT',看看它是否會有所作爲,而事實並非如此。 :( –

+0

其實,如果您使用Intent.createChooser,谷歌照片是否會正常返回? –

+0

是的,我認爲是這樣,但我不想創建一個選擇器,我想嚴格限制來自GooglePhotos的輸入 –

1

你可以只使用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); 
相關問題