3

I am trying to launch the gallery from my app when user clicks on the notification. I have found that it is only possible if you know the package and the class name of the Gallery app. I have managed to find the same for four device manufacturers, and so far this code works. I just need the package and class name for Motorola and LG Android phones.

Can anyone help? It is very easy for you if you are a developer and own a Motorola or LG Android device. You just need to launch gallery in your phone while connected to LogCat, and it will show the package and class name of the Gallery.

CODE:

Intent newIntent = new Intent();

//open Gallery in Nexus plus All Google based ROMs
if(doesPackageExist("com.google.android.gallery3d"))
    newIntent.setClassName("com.google.android.gallery3d", "com.android.gallery3d.app.Gallery");

//open Gallery in Sony Xperia android devices
if(doesPackageExist("com.android.gallery3d"))
    newIntent.setClassName("com.android.gallery3d", "com.android.gallery3d.app.Gallery");

//open gallery in HTC Sense android phones
if(doesPackageExist("com.htc.album"))                           
    newIntent.setClassName("com.htc.album", "com.htc.album.AlbumMain.ActivityMainCarousel");

//open gallery in Samsung TouchWiz based ROMs
if(doesPackageExist("com.cooliris.media"))
    newIntent.setClassName("com.cooliris.media", "com.cooliris.media.Gallery");

startActivity(newIntent);

And to check if package name exists:

public boolean doesPackageExist(String targetPackage) {

    PackageManager pm = getPackageManager();
    try {
        PackageInfo info = pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        return false;
    }
    return true;    
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
vividh
  • 517
  • 7
  • 11

2 Answers2

4

You should be able to start the Gallery app via a basic Intent like this:

Intent intent =  new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivity(intent);

It may fire the app picker if more than one app is able to let you display images (e.g. Gallery and ESFileExplorer).

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • 1
    That's the point, I don't want user to pick any image, I want him to just land straight to the Gallery app. – vividh Oct 25 '12 at 10:41
  • 1
    This is usually not what Android users expect. If they have set MyFooView as their preferred image viewing app, they will be mad at you if you unconditionally launch the one you want. – Heiko Rupp Oct 25 '12 at 10:43
  • 2
    Nope, not unconditionally, only when they click on the notification. I show the notification when user downloads photos from my download utility and on clicking it, the user is taken to gallery to show the downloaded photos. – vividh Oct 25 '12 at 10:52
1

There is no universal table describing the "Gallery" app on every Android device, so the best you can do to avoid showing the user the activity resolver is to list all of the possible activity handlers programmatically and make an informed guess about which one to launch.

PackageManager.queryIntentActivities turns an Intent into such a list of packages as long as you seed the Intent with the type of file to open:

Intent newIntent = new Intent(Intent.ACTION_VIEW);
newIntent.setType("image/*");
List<ResolveInfo> allHandlers = pm.queryIntentActivities(newIntent, PackageManager.MATCH_DEFAULT_ONLY);

You could then trawl this list for known packages (from your list above) or, failing that, launch the first one in the list.

However, you should consider making a trivial Activity of your own to display the image. That is the only way to gain the level of control you seek.

Rupert Rawnsley
  • 2,622
  • 1
  • 29
  • 40