Launching Gallery in android phones

2020-03-28 15:28发布

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

2条回答
ら.Afraid
2楼-- · 2020-03-28 15:29

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.

查看更多
迷人小祖宗
3楼-- · 2020-03-28 15:56

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).

查看更多
登录 后发表回答