Android: How to share image with text on facebook via intent?

后端 未结 7 2365
眼角桃花
眼角桃花 2020-11-27 03:18

I\'d like to share a photo with caption pre-filled from my app via a share intent, on facebook.

Example code

Intent intent = new Intent();
intent.set         


        
相关标签:
7条回答
  • 2020-11-27 04:00

    The newest Facebook versions doesn't allow you to share text using intents. You have to use the Facebook SDK to do it - to make that simple, use the Facebook SDK + Android Simple Facebook (https://github.com/sromku/android-simple-facebook). Using the library, your code would be like this (extracted from the Simple Facebook site):

    Publish feed

    Set OnPublishListener and call for:

    • publish(Feed, OnPublishListener) without dialog.
    • publish(Feed, true, OnPublishListener) with dialog.

    Basic properties

    • message - The message of the user
    • name - The name of the link attachment
    • caption - The caption of the link (appears beneath the link name)
    • description - The description of the link (appears beneath the link caption)
    • picture - The URL of a picture attached to this post. The picture must be at least 200px by 200px
    • link - The link attached to this post

    Initialize callback listener:

    OnPublishListener onPublishListener = new OnPublishListener() {
        @Override
            public void onComplete(String postId) {
                Log.i(TAG, "Published successfully. The new post id = " + postId);
            }
    
         /* 
          * You can override other methods here: 
          * onThinking(), onFail(String reason), onException(Throwable throwable)
          */
    };
    

    Build feed:

    Feed feed = new Feed.Builder()
        .setMessage("Clone it out...")
        .setName("Simple Facebook for Android")
        .setCaption("Code less, do the same.")
        .setDescription("The Simple Facebook library project makes the life much easier by coding less code for being able to login, publish feeds and open graph stories, invite friends and more.")
        .setPicture("https://raw.github.com/sromku/android-simple-facebook/master/Refs/android_facebook_sdk_logo.png")
        .setLink("https://github.com/sromku/android-simple-facebook")
        .build();
    

    Publish feed without dialog:

    mSimpleFacebook.publish(feed, onPublishListener);
    

    Publish feed with dialog:

    mSimpleFacebook.publish(feed, true, onPublishListener);
    


    Update on 14 December 2015


    according to New Facebook SDK.

    facebook-android-sdk:4.6.0

    It's very Simple.
    1. create Provider in Android.manifest.xml

    <provider
                android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"
                android:name="com.facebook.FacebookContentProvider"
                android:exported="true" />
    

    2. Create Your Share Intent with Data.

    ShareHashtag shareHashTag = new ShareHashtag.Builder().setHashtag("#YOUR_HASHTAG").build();
    ShareLinkContent shareLinkContent = new ShareLinkContent.Builder()
                    .setShareHashtag(shareHashTag)
                    .setQuote("Your Description")
                    .setContentUrl(Uri.parse("image or logo [if playstore or app store url then no need of this image url]"))
                    .build();
    


    3. Show The Share Dialog

    ShareDialog.show(ShowNavigationActivity.this,shareLinkContent);
    


    That's It.

    0 讨论(0)
  • 2020-11-27 04:00

    Add these lines to your following code

    shareCaptionIntent.putExtra(Intent.EXTRA_TITLE, "my awesome caption in the EXTRA_TITLE field");
    
    0 讨论(0)
  • 2020-11-27 04:02

    In this Formula you can share image both Messenger and Instagram(com.instagram.android) without using any Provider in "AndroidManifest

    public void shareMessenger(View v) {
        // showToast("checking");
    
        File dir = new File(Environment.getExternalStorageDirectory(), "MyFolder");
    
        File imgFile = new File(dir, "Image.png");
    
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setType("image/*");
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imgFile));
        sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->.");
        sendIntent.setPackage("com.facebook.orca");
        sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            startActivity(Intent.createChooser(sendIntent, "Share images..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(SaveAndShareActivity.this, "Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
        }
    
    }
    

    **Add this two line in onCreate Method **

     StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
    
    0 讨论(0)
  • 2020-11-27 04:06

    Without using Facebook sdk we can't share the image and text simultaneously on facebook. To solve this problem I had create a bitmap of image and text, Share that bitmap on facebook and it's working perfectly.

    You can download the source code from here (Share image and text on facebook using intent in android)

    Here is code:

    MainActivity.java

    package com.shareimage;
    
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener {
    EditText et_text;
    ImageView iv_image;
    TextView tv_share,tv_text;
    RelativeLayout rl_main;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        init();
    
    }
    
    private void init(){
        et_text = (EditText)findViewById(R.id.et_text);
        iv_image = (ImageView)findViewById(R.id.iv_image);
        tv_share = (TextView)findViewById(R.id.tv_share);
        rl_main = (RelativeLayout)findViewById(R.id.rl_main);
        tv_text= (TextView) findViewById(R.id.tv_text);
    
        File dir = new File("/sdcard/Testing/");
        try {
            if (dir.mkdir()) {
                System.out.println("Directory created");
            } else {
                System.out.println("Directory is not created");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        tv_share.setOnClickListener(this);
    
        et_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                tv_text.setText(et_text.getText().toString());
    
            }
        });
    
    
    }
    
    
    
    
    @Override
    public void onClick(View v) {
    
        switch (v.getId()){
            case R.id.tv_share:
                Bitmap bitmap1 = loadBitmapFromView(rl_main, rl_main.getWidth(), rl_main.getHeight());
                saveBitmap(bitmap1);
                String str_screenshot = "/sdcard/Testing/"+"testing" + ".jpg";
    
                fn_share(str_screenshot);
                break;
        }
    
    }
    
    public void saveBitmap(Bitmap bitmap) {
        File imagePath = new File("/sdcard/Testing/"+"testing" + ".jpg");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
    
            Log.e("ImageSave", "Saveimage");
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }
    
    public static Bitmap loadBitmapFromView(View v, int width, int height) {
        Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
    
        return b;
    }
    
    public void fn_share(String path) {
    
        File file = new File("/mnt/" + path);
    
        Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
    
        startActivity(Intent.createChooser(intent, "Share Image"));
    
    
    }
    
    }
    
    0 讨论(0)
  • 2020-11-27 04:07
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    
       shareIntent.setType("image/*");
    
       shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
    
       shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); // put your image URI
    
       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;
            }
          }
    
    0 讨论(0)
  • 2020-11-27 04:10

    FB no longer allows you to prefill the sharing message.

    In order to circumvent this, you will need to use an SDK to publish via a Graph request. For this you will need the publish_actions permission. Since last month you need to submit your app to a review process to gain access to publish_actions. Which you would fail if your app prefills the sharing texts. Trust me - I've had the Chutzppah to try.

    So it looks like we would have to comply.

    B.t.w. in iOS you can still prefill the texts using the FB sdk. Who knows for how long.

    0 讨论(0)
提交回复
热议问题