2016-09-26 10 views
1

問題があります。異なる電話機でテストを実行しているときに、ギャラリーを開くときに機能していません。デフォルト画像opennerのパッケージ名を取得

ここで、デフォルトのギャラリーオープナーアプリのパッケージ名を取得したいので、コードで使用できます。これをプログラムで行うにはどうすればよいですか?ここで

は基本的に、私がする必要があるすべてはイメージのためのアプリケーションのパッケージ名を取得することです5.

Resources resources = InstrumentationRegistry.getTargetContext().getResources(); 
     Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 
       resources.getResourcePackageName(R.drawable.ic_launcher) + '/' + 
       resources.getResourceTypeName(R.drawable.ic_launcher) + '/' + 
       resources.getResourceEntryName(R.drawable.ic_launcher)); 
     Intent resultData = new Intent(); 
     resultData.setData(imageUri); 
     Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData); 
     intending(toPackage("com.google.android.apps.photos")).respondWith(result); 
     //Click the select button 
     onView(withId(R.id.register_image)).perform(click()); 
     threadSleep(MILISECONDS_TIMEOUT); 
     onView(withText("From Gallery")).perform(click()); 
     threadSleep(MILISECONDS_TIMEOUT); 
     Spoon.screenshot(getActivityInstance(), "picture_selected"); 

ネクサス上でテストを実行しているときに私が使用しています方法です。

答えて

2

だから、あなたはこのような何かをしたいと思うでしょう:

次の方法utilsのクラスを作成し、追加します。今

public static String getPackageForGalery() { 
      Intent mainIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      mainIntent.setType("image/*"); 
      List<ResolveInfo> pkgAppsList = getApplicationContext().getPackageManager().queryIntentActivities(mainIntent, PackageManager.GET_RESOLVED_FILTER); 
      int size = pkgAppsList.size(); 
      for (ResolveInfo infos : pkgAppsList) { 
       return infos.activityInfo.processName; 

      } 
      return null; 
     } 

を、あなたのコードでは、次の操作を行います。

Resources resources = InstrumentationRegistry.getTargetContext().getResources(); 
     Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 
       resources.getResourcePackageName(R.drawable.ic_launcher) + '/' + 
       resources.getResourceTypeName(R.drawable.ic_launcher) + '/' + 
       resources.getResourceEntryName(R.drawable.ic_launcher)); 
     Intent resultData = new Intent(); 
     resultData.setData(imageUri); 
     Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData); 
     intending(toPackage(YourUtilsClass.getPackageForGalery())).respondWith(result); 
     //Click the select button 
     onView(withId(R.id.register_image)).perform(click()); 
     threadSleep(MILISECONDS_TIMEOUT); 
     onView(withText("From Gallery")).perform(click()); 
     threadSleep(MILISECONDS_TIMEOUT); 
     Spoon.screenshot(getActivityInstance(), "picture_selected"); 
1

PackageManagerはあなたの友人です。具体的には、あなたのタイプの意図を聞いているすべてのアプリケーションのリストを取得するには、 queryIntentActivities(Intent intent, int flags)を使用できます。これにより、あなたの意図に合った活動のリストが返されます。リゾルバと一致しない場合、リストは空になります。これは、あなたの意図が適切に形成されていない可能性があります。

関連する問題