Android how to get name of app used to share - java

I'm using the following method to share an image with any app the user picks.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
File imageFileToShare = new File(imagePath);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFileToShare));
if (!TextUtils.isEmpty(text)) {
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
}
context.startActivity(Intent.createChooser(shareIntent, "Share with").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
This works fine, but now I'm trying to get the name of the app picked by the user for sharing. Is there a (standard) way to do this?
Thanks for the attention.
Jose

This is not supported prior to Android 6.0, when using the Android chooser. On Android 6.0+, you can use EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER to provide an IntentSender that will be notified when the user makes a choice and what that choice is.
You are welcome to use PackageManager and queryIntentActivities() to find out what activities support your shareIntent and create your own UI for the user to choose from. Then, since it is your own UI, you will find out what the user chose.

You can try with this code:
PackageManager packageManager = getPackageManager();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
getNameFromApp(packageManager);
And
public String getNameFromApp(PackageManager packageManager) {
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(shareIntent, 0);
int lastDot = 0;
String name = "";
for (int i = 0; i < resolveInfos.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo resolveInfo = resolveInfos.get(i);
String packageName = resolveInfo.activityInfo.packageName;
lastDot= packageName.lastIndexOf(".");
name = packageName.substring(lastDot + 1);
}
return name;
}
This code gets last app's name. But You can do it however you want to. Just do it!.

Related

Instagram application does not open with Android Intent

This is my code, the thing is I don't want to send images I just want the IG app to open. Yes the app is installed but still it is not getting picked up.
This was working fine till last week. Please help.
Uri uri = Uri.parse("http://instagram.com/");
Intent insta = new Intent(Intent.ACTION_VIEW, uri);
insta.setPackage("com.instagram.android");
if (isIntentAvailable(insta)) {
this.startActivityForResult(insta, REQUEST_CODE_MY_PICK);
} else {
}
private boolean isIntentAvailable(Intent intent) {
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
Try this
Uri appUri = Uri.parse("https://instagram.com/_u/user_name");
Uri browserUri = Uri.parse("https://instagram.com/user_name");
try{ //first try to open in instagram app
Intent appIntent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if(appIntent != null){
appIntent.setAction(Intent.ACTION_VIEW)
appIntent.setData(appUri);
startActivity(appIntent);
}
}catch(Exception e){ //or else open in browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW, browserUri);
startActivity(browserIntent);
}
Try to replace Uri.parse("http://instagram.com/") with Uri.parse("https://instagram.com/"). Also try to remove the isIntentAvailable and just call startActivity(insta) without 'forResult' and lastly remove insta.setPackage line.

How to open my Image in one specif application from the list of different application?

I want to open the image in the specific application by default from the list of different gallery application. I am able to get the list of available application.
But I want to choose one specific Application by default without displaying the list of application. it is possible in Phone setting but I want to do it programmatically.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
PackageManager pm = getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(intent, 0);
List<String> intentList = new ArrayList<>();
String packageName = null;
for (int i=0;i<resInfo.size();i++){
ResolveInfo ri = resInfo.get(i);
packageName = ri.activityInfo.packageName;
intentList.add(packageName);
}
startActivity(intent);
Thanks for the help in Advance.....
You need to pass in the app's package name in setPackage() when creating your intent.
intent.setPackage("com.example.gallery");
startActivity(Intent.createChooser(intent, "View Image"));

How can I install Android PlayStore application from another application programmatically?

I am new to creating android application and I am working on project where I need to download and install application from Android Playstore and install without user input. The only solution i could find is to launch market app and then click install button manually. But I want to install automatically. Is there any better way?
Installation of apps without user input isn't possible, but you can ask user to install it by following intent:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(GOOGLE_PLAY_APP_URL));
startActivity(intent);
And for check if app is already installed you can use something like this:
public static boolean isInstalled(Context context, String packageName) {
PackageManager pkgManager = context.getPackageManager();
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resInfo = pkgManager.queryIntentActivities(launchIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String pkgName = ri.activityInfo.packageName;
if (pkgName.contains(packageName)) {
return true;
}
}
return false;
}

Sharing text with image to instagram using android intent

I know that this question has been asked several times before, I am trying to add caption to image shared to instagram using send intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"YOUR TEXT TO SHARE IN INSTAGRAM");
shareIntent.setPackage("com.instagram.android");
return shareIntent;
Has someone ever managed to make it work?
Is it not supported or has the support been revoked?
There was an official statement from Instagram (mid-2015) announcing that pre-populated captions would no longer be accepted in the iOS and Android apps:
Beginning today, the iOS Hooks and Android Intents will stop accepting captions passed by third party apps. This is a non-breaking change: existing mobile apps that utilize pre-filled captions will continue to be able to use this flow to share media through the Instagram apps, but now Instagram will ignore the caption text. To create a caption for a photo or video shared by a third party app, users will have to enter a caption manually, the same way they already do when sharing content using the Instagram native apps.
Looking at the Instagram documentation for Android, indeed we see that there's no mention of providing the conventional Intent.EXTRA_TEXT string extra in the intent as is customary for other apps. Their sample is limited to only providing a Uri:
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
I'm sorry to say that it simply isn't possible, and we're at the discretion of Facebook in making this decision.
Until it`s not solved by Instagram, I copy the text to the clipboard and instruct the user to paste it
#Override
public void onSingleImageSelected(Uri uri, String tag) {
fileProfileImage = uri.getPath();
compressProfileImage();
imgShareTosocial.setVisibility(View.VISIBLE);
Glide.with(getApplicationContext()).load(uri).into(imgShareTosocial);
}
#SuppressLint("CheckResult")
private void compressProfileImage() {
File file = new File(fileProfileImage);
new Compressor(this)
.compressToFileAsFlowable(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<File>() {
#Override
public void accept(File file) throws Exception {
compressProfileImage = file;
String imagePath = compressProfileImage.getAbsolutePath();
tvSelectMedia.setText(imagePath);
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
}
});
}
private void shareToInstagram() {
path = tvSelectMedia.getText().toString().trim();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if (intent != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
try {
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), path, "Step Up", "Step Up")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
shareIntent.setType("image/jpeg");
startActivity(shareIntent);
} else {
// bring user to the market to download the app.
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.instagram.android"));
startActivity(intent);
}
}
I'm with the same problem. I think is not possible at this time.
In https://instagram.com/developer/mobile-sharing/android-intents/ only talk about Intent.EXTRA_STREAM, so i suppose that it's the only available.
Here is my code:
Intent instagramIntent = new Intent(Intent.ACTION_SEND);
instagramIntent.setType("image/*");
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
instagramIntent.putExtra(Intent.EXTRA_STREAM, uri);
instagramIntent.setPackage("com.instagram.android");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(instagramIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
if(resolveInfo.activityInfo.packageName.startsWith("com.instagram.android")){
instagramIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name );
resolved = true;
break;
}
}
if(resolved){
startActivity(instagramIntent);
}else{
Toast.makeText(PromocionarMain.this, "Instagram App is not installed", Toast.LENGTH_LONG).show();
}
Instagram have stopped accepting pre-populated capitions to increase the quality of content in the system. See this post.
http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing

Android Share Intent chooser - Sharing TEXT with Facebook/Twitter Social media etc

If I only send text, the Share Intent chooser DOES NOT give Facebook/Twitter as an option.
Only Gmail, Skype and Evernote are options.
Here is my code
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("plain/text");
shareIntent.putExtra(Intent.EXTRA_TEXT, text)
startActivity(Intent.createChooser(shareIntent, "Share using"));
I have tried different combination of setType() with no joy including "text/*", "text/html" and passing HTML text in the putExtra as follows:
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text that will be shared.</p>"));
When I use "text/plain", Facebook comes up as an option but the text does not load up when selecting it. But the text does load for Twitter,email, SMS.
Has anyone else encountered this problem?
When I share an image, there is no problem and Facebook along with other social media apps are available on the list.
Also it should be "text/plain" and not "plain/text" according to the documentation.
this depends upon what intent filters are defined by each of those apps.
For instance if I add intent-filter
android.intent.action.send
If I choose single image from Gallery my application will appear in the list. However if I choose multiple my application will not appear as I have not added the intent-filer for android.intent.action.send_multiple
So it depends upon what intents facebook is filtering for. You need to see the release notes or help or developer pages for that.
The facebook issue is a limitation in facebook permissions. Use the facebook API
Share via Twitter :
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)v.getTag(R.drawable.ic_launcher));
// for finding twitter package name ---- >>
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Facebook
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)
v.getTag(R.drawable.ic_launcher));
// finding facebook package name
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Gmail
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT(String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT(String)v.getTag(R.drawable.ic_launcher));
// finding gmail package name ---
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("gmail"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}

Categories