Launching Gallery in android phones

删除回忆录丶 提交于 2019-12-12 07:28:09

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/13066013/launching-gallery-in-android-phones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!