Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import java.util.List;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import android.net.Uri;

import android.util.Log;

public class Main {
    private static void shareImageOnFacebook(String imagePath, Context context) {

        Log.d("CitationsManager-ShareOnFb", "sharing the image " + imagePath);
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        // shareIntent.putExtra(Intent.EXTRA_TEXT, "www.google.com");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)));
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
        for (final ResolveInfo app : activityList) {

            Log.d("CitationsManager-ShareOnFb", app.activityInfo.name);
            if ((app.activityInfo.name).contains("com.facebook") && !(app.activityInfo.name).contains("messenger")
                    && !(app.activityInfo.name).contains("pages")) {
                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
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                shareIntent.setComponent(name);
                context.startActivity(shareIntent);
                break;
            }
        }

    }
}