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;
}
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).
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.
Related
Background
In my Android App, users can share generated images to other apps. It's working nicely using the ACTION_SEND Intent.
Many users have asked why they can't share to Instagram stories directly.
Initially I thought Instagram doesn't support receiving Intents for stories (correct to some extent). I searched for it today, and according to this documentation, to share to Instagram Stories, a separate intent com.instagram.share.ADD_TO_STORY has to be used. I tried it, and it works fine.
The problem:
How do I keep both the options available?
I thought about it a lot, and came up with the following options:
1) Have two separate buttons. It will work, but it will look/feel bad.
2) Have my app accept ACTION_SEND intent, name it as Share to Instagram Story, and redirect the intent to the com.instagram.share.ADD_TO_STORY intent. In principle, make a proxy intent.
It will work, and look/feel great, but I don't know if its allowed (legal, etc) and can I disable the intent if the user doesn't have Instagram installed.
3) Add the 'com.instagram.share.ADD_TO_STORY' to the app chooser launched by ACTION_SEND. This would be ideal, but I don't know how to do it.
If you want to add multiple actions to Intent and create a chooser look at this example:
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent chooserIntent = Intent.createChooser(editIntent, "Open in...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { viewIntent });
startActivity(chooserIntent);
UPDATE: Here is good solution to your answer. How to make an intent with multiple actions
I tried the same approach as on Facebook's official documentation then tested on Huawai P9 Lite, Huawai P20 Lite and on Samsung S8 - it only worked on Samsung S8 for not known reason (to me). I gave up on it since, obviously, it's not working on most of the phones.
// Define image asset URI
Uri stickerAssetUri = Uri.parse("your-image-asset-uri-goes-here");
String sourceApplication = "com.my.app";
// Instantiate implicit intent with ADD_TO_STORY action,
// sticker asset, and background colors
Intent intent = new Intent("com.instagram.share.ADD_TO_STORY");
intent.putExtra("source_application", sourceApplication);
intent.setType(MEDIA_TYPE_JPEG);
intent.putExtra("interactive_asset_uri", stickerAssetUri);
intent.putExtra("top_background_color", "#33FF33");
intent.putExtra("bottom_background_color", "#FF00FF");
// Instantiate activity and verify it will resolve implicit intent
Activity activity = getActivity();
activity.grantUriPermission("com.instagram.android", stickerAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (activity.getPackageManager().resolveActivity(intent, 0) != null) { activity.startActivityForResult(intent, 0);}
I am building an app that auto sorts installed apps and allows the user to launch those apps. I just ran into a small issue with the camera app. When I scan through the installed apps with 'queryIntentActivities' for 'ResolveInfo' I get the gallery ResolveInfo twice and both objects are identical. I am wondering if I am missing something obvious or the camera is simply not launch-able from a package name.
If the camera and the gallery share the same package would I still be able to use the package manager to pull the icons and labels for each? I want to avoid using my own drawable as different OEMs make different icons.
I am aware that I can simply launch the camera with a capture image intent but I do not care for the results and I want to launch the camera as a stand alone activity plus using this intent doesn't really solve my issue.
Update:
So the ResolveInfo's I am receiving for the camera and gallery aren't exactly the same. They share the same package name but I can pull their respective icons through the ResolveInfo.loadLabel and ResolveInfo.loadIcon (rather then what I was doing with ResolveInfo.ApplicationInfo.loadLabel ... which returned identical labels and icons). However I am still unable to find any way to launch the activities for the camera and gallery separately.
Update 2.0
Problem solved. I found the unique activity string in the ResolveInfo.ActivityInfo.name. Now rather then launching the app with the package name I just launch it with the activity that was listed in that variable.
No, the package manager for camera and gallery are different.
for camera - com.android.camera.
for gallery - com.android.gallery.
this is the way in which you would differentiate the gallery and camera.
The best way to get the package name of the camera app is pasted below. You can get the package name by refering to cameraInfo.activityInfo.packageName; returned by the function below
public static ResolveInfo getCameraPackageName(Context context, PackageManager pm) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
ResolveInfo cameraInfo = null;
List<ResolveInfo> pkgList = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if(pkgList != null && pkgList.size() > 0) {
cameraInfo = pkgList.get(0);
}
return(cameraInfo);
}
I'm opening a PDF file through an intent; in order to check beforehand if a capable application is installed, I use this code:
PackageManager packageManager = context.getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List<ResolveInfo> list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
This works fine on my Galaxy Nexus, however, the list is empty when run on a Kindle Fire. But if I skip the check, the PDF is opened just fine! How so?
Is there any other way to check for PDF viewing capablities? (The PDF is included in the APK asset's folder, and I would like to only copy it into the data folder if I know it can be displayed...)
Or should I just stop bothering to check as most Android devices can view PDFs anyway?
I'm fairly sure you can't query for that intent without setting the actual data the Intent should be able to operate on.
This link also seems to deal with the same problem.
I would like to start a default application: browser, contact-book, phone, email, music app, etc. I have found many q/a, like browser opening a specific URL or blank, and here the answer is even "No not possible". But I would like to just open/launch it without telling it to go to a specific URL or sending a mail to someone, etc.
However, I also saw some Home applications where this seems to be working (at least for some apps). On my colleague's device there is for example a different contact-book (no google) which is detected and opened correctly.
I have seen in the Android documentation some intent categories that point to these problems, but these are only >= API.11. So I can't use/test them on my device.
Question: Is it not somehow possible to launch a default application (having the app chooser is of course ok) without providing extra data? If no, what do you think are these Home apps doing (perhaps workarounds are somehow possible).
PS: for the phone app I think, I have a workaround using Intent.ACTION_DIAL without any other information which will open simply the dialer.
UPDATE: I modified the title. Some applications like the address book may not be the same on different devices. So in this case I would like to start the address-book app, whichever this is.
This answer is not a 100% answer, but some workarounds on some typical applications.
Still open are: music player, address book
Browser: I get a list of applications that handle "http"-data intents, and then I look if one is available in the list of preferred applications.
Intent appFilter = new Intent(Intent.ACTION_VIEW);
appFilter.setData(Uri.parse("http://www.google.com"));
List<ResolveInfo> browserInfoList = pm.queryIntentActivities(appFilter, 0);
List<IntentFilter> outFilters = new ArrayList<IntentFilter>();
List<ComponentName> outActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(outFilters, outActivities, null);
if(outActivities.size() > 0) {
for(ComponentName cn : outActivities) {
String cnClass = cn.getClassName();
String cnPkg = cn.getPackageName();
for (ResolveInfo info : browserInfoList) {
if(info.activityInfo.name.equals(cnClass) &&
info.activityInfo.packageName.equals(cnPkg)) {
return cn;
}
}
}
}
In case no default is found, I open a browser chooser dialog, see here.
Phone: as described in the question:
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
You can start apps by the function "startActivity" if you know about the canonical app name
like "android.com.browser". Do this simple by searching for AndroidManifest.xml in the app
source code (look at Codeaurora.com or at github/Cyanogenmod) and grab the app name you want.
After you know about the App name ("Activity") implement the code as follows:
Intent intent = new Intent();
intent.setClassName(this, "com.android.browser");
intent.setCategory(Intent.ACTION_MAIN);
startActivity(intent);
THIS is only a example, sometimes you have to put intent extras or data values, this information can be found in the app's AndroidManifest.xml too.
How could I write a code which can tell me that android market is installed on your android phone?
There are two ways. You can use the already mentioned getPackageManager() and getApplicationInfo() (if the package is not found, a PacketManager.NameNotFoundException will be thrown - see here). Android Market's package name is com.android.vending.
However, you can also create a dummy intent for searching the market and check how it is handled. If the resulting list has at least one entry, you can be sure that Android Market is installed:
Intent market = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=dummy"));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(mmarket, 0);
Here's what I did (assumes browser exists):
Intent market = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("market://details?id=com.example.app"));
Intent website = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("http://play.google.com/store/apps/details?id=com.example.app"));
try {
startActivity(market);
} catch (ActivityNotFoundException e) {
startActivity(website);
}