Rajesh Panchal
Rajesh Panchal

Reputation: 1170

Sharing image to facebook wall from sd card by normal way and also by Facebook SDK

i want to share image to my facebook wall, for that i am taking the image from sd card and then sharing, my code :

String dirpath = android.os.Environment.getExternalStorageDirectory().toString();
String path = dirpath+"/test.png";
Uri imageUri = Uri.parse(path);

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sharingIntent.setType("image/png");
startActivity(Intent.createChooser(sharingIntent , "Send image using.."));

but its not working, please give me some solution.

Also i want code for sharing image using Facebook SDK..

Upvotes: 0

Views: 580

Answers (2)

HK1988
HK1988

Reputation: 434

It Was My Problem Too

you used

Intent.FLAG_ACTIVITY_NEW_TASK

so it means you are not in activity

And Your Error Is:

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

you should make Global Class so what's that?

create class for example its name is G.java

public class G extends Application { }

extends application

and Define it in "AndroidManifest.xml"

    <application
    android:name=".G"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

And Create :

public static Activity              currentActivity;

"currentActivity" is Variable save your Current Activity

in onResume Event of your activity write:

    G.currentActivity = this;
    super.onResume();

Attention: it must place before super.onresume();

So in your code use:

                    G.currentActivity.startActivity(Intent.createChooser(sharingIntent, "Share via..."));


    G.currentActivity.

used before :

    startActivity(Intent);

Upvotes: 1

sam
sam

Reputation: 56

protected void share(String nameApp, String imagePath, String text) {
        // TODO Auto-generated method stub

        try {
            List<Intent> targetedShareIntents = new ArrayList<Intent>();
            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setType("image/jpeg");
            List<ResolveInfo> resInfo = getPackageManager()
                    .queryIntentActivities(share, 0);
            if (!resInfo.isEmpty()) {
                for (ResolveInfo info : resInfo) {
                    Intent targetedShare = new Intent(
                            android.content.Intent.ACTION_SEND);
                    targetedShare.setType("image/jpeg"); // put here your mime
                    // type
                    if (info.activityInfo.packageName.toLowerCase().contains(
                            nameApp)
                            || info.activityInfo.name.toLowerCase().contains(
                                    nameApp)) {
                        targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
                        targetedShare.putExtra(Intent.EXTRA_TEXT, text);
                        targetedShare.putExtra(Intent.EXTRA_STREAM,
                                Uri.fromFile(new File(imagePath)));
                        targetedShare.setPackage(info.activityInfo.packageName);
                        targetedShareIntents.add(targetedShare);
                    }
                }
                Intent chooserIntent = Intent.createChooser(
                        targetedShareIntents.remove(0), "Select app to share");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                        targetedShareIntents.toArray(new Parcelable[] {}));
                startActivity(chooserIntent);
            }
        } catch (Exception e) {
        }

    }

Upvotes: 3

Related Questions