1
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");

Uri uri = Uri.parse(pathToImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;

我在上面的代码中用于在社交网站上共享图像。当我在 facebook 上发布图像时,只发布文本并且图像没有出现。我们如何获取图像和 pathtoimage 是字符串变量我正在获取服务器图像路径并存储在字符串变量中.但它不起作用。请为我的上述问题提供任何解决方案。?

4

3 回答 3

4

尝试这个

Bitmap icon = mBitmap;
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {                       
            e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
    startActivity(Intent.createChooser(share, "Share Image"));

参考更多链接

http://android-developers.blogspot.in/2012/02/share-with-intents.html

Android 通过对话框分享

图片从 android sdcard 发布到 facebook 墙上

于 2013-10-07T06:27:44.123 回答
2

这是我想出的解决方案:如果您知道包名称,此功能允许您分享到 instagram、facebook 或任何其他应用程序。如果未安装该应用程序,它会重定向到 Play 商店。如果您将“其他”作为参数发送,您将显示一个也支持图像共享的应用程序列表。

shareImageIntent(mActivity,"image/*", "/storage/emulated/0/Pictures/Instagram/IMG_20150120_095603.jpg", "Instagram Sharing test. #Josh","com.instagram.android"); // parameters - for facebook: "com.facebook.katana" , to display list of other apps you can share to: "others"

 public void shareImageIntent(Activity a,String type, String mediaPath, String caption, String app){
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(app);
        if ((intent != null)||(app.equals("others"))){

            // Create the new Intent using the 'Send' action.
            Intent share = new Intent(Intent.ACTION_SEND);

            if(!app.equals("others"))
                share.setPackage(app);

            // Set the MIME type
            share.setType(type);    

            // Create the URI from the media
            File media = new File(mediaPath);
            Uri uri = Uri.fromFile(media);

            // Add the URI and the caption to the Intent.
            share.putExtra(Intent.EXTRA_STREAM, uri);
            share.putExtra(Intent.EXTRA_TEXT, caption);
            // Broadcast the Intent.
            //startActivity(Intent.createChooser(share, "Share to"));
            a.startActivity(share);
            }
        else{
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
            intent.setData(Uri.parse("market://details?id="+app));
            a.startActivity(intent);            
            }   
        }
于 2015-01-20T09:26:23.873 回答
1

您不能使用 Intent 发布到 Facebook。他们专门阻止了这一点。您必须使用他们的 SDK 或将其复制到剪贴板并让用户在 facebook 界面打开后将其粘贴。我有同样的问题。

于 2014-07-18T18:49:10.847 回答