Miljan Vulovic Miljan Vulovic - 2 months ago 17
Android Question

Get package name of default image openner

I have a problem. When running my tests on different phones, when trying to open gallery, it is not working.

Now, what I want to do is to get package name for the default gallery opener app, so I can use it in my code. How can I do this programmatically?

Here is how am I using when running tests on Nexus 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");


Basically, all i need to do is to get package name of the application for images.

Answer

So, you will want to do something like this:

Create utils class and add the following method:

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

Now, in your code, do this:

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");