Android分享意图在某些应用中无效

时间:2016-11-18 09:27:18

标签: android facebook sharing share-intent

我必须将图像与文本共享给所有社交媒体。所以我尝试了以下代码: -

 share.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Uri uri = imageUrl;

                Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                shareIntent.setType("text/html");
                shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)
                        v.getTag(R.string.app_name));
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                        "Text for post");
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                context.startActivity(Intent.createChooser(shareIntent, "Share Using"));

            }
        });

它正在运行,现在我可以在gmail中与图像共享文本,就像应用程序一样。但问题是即使我已经安装并更新了所有这些应用程序,我也无法使用此共享意图来获取Facebook,Twitter和Instagram。

我需要获取所有社交媒体应用以进行共享。

使用" text / plain"作为shareIntent类型Facebook出现但无法共享图像...

有人可以帮我找到答案吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

试试这个为我工作,

public partial class Form1 : Form
{

    List<double> values_1 = new List<double>();
    List<double> values_2 = new List<double>();

    public Form1()
    {
        InitializeComponent();

        make_values();

        for (int i = 0; i < values_1.Count; i++)
        {
            chart1.Series[0].Points.AddY(values_1[i]);
        }

        for (int i = 0; i < values_2.Count; i++)
        {
            chart1.Series[1].Points.AddY(values_2[i]);
        }

        // set the colour of grid to corresponding line
        chart1.ChartAreas[0].AxisY2.MajorGrid.LineColor = chart1.Series[1].Color;

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void make_values()
    {
        for (int i = 0; i < 600; i++)
        {
            values_1.Add(Math.Sin(i / 60.0));
            values_2.Add(Math.Cos(i / 60.0));
        }

    }
}

void share(String nameApp, Uri imagePath) { boolean isAppExist = false; try { List<Intent> targetedShareIntents = new ArrayList<Intent>(); Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/*"); List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0); if (!resInfo.isEmpty()) { for (ResolveInfo info : resInfo) { Intent targetedShare = new Intent(Intent.ACTION_SEND); targetedShare.setType("image/*"); // put here your mime type if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) { targetedShare.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_title)); targetedShare.putExtra(Intent.EXTRA_TEXT, shareMessage); if (imagePath != null) targetedShare.putExtra(Intent.EXTRA_STREAM, imagePath); targetedShare.setPackage(info.activityInfo.packageName); targetedShareIntents.add(targetedShare); isAppExist = true; } } Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{})); startActivityForResult(chooserIntent, REQUEST_SHARE); } } catch (Exception e) { Utils.setLog("Exception while sending image on" + nameApp + " " + e.getMessage()); } if (!isAppExist) { Dialogs.showAlert(this, null, getString(R.string.share_no_application_found), true, false); } } 中,您必须传递您必须在其中发布图片的应用程序名称。在你的情况下通过nameApp

答案 1 :(得分:0)

尝试以下代码:

 public void share(final String url, final String text) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/jpeg");
                share.putExtra(
                        Intent.EXTRA_TEXT,
                        "Sharing from "
                                + context.getString(R.string.app_name)
                                + "\n" + text);

                if (url != null) {
                    Bitmap bmp = null;
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                    try {
                        byte[] chunk = new byte[4096];
                        int bytesRead;
                        InputStream stream = new URL(url).openStream();

                        while ((bytesRead = stream.read(chunk)) > 0) {
                            outputStream.write(chunk, 0, bytesRead);
                        }

                        outputStream.toByteArray();

                        bmp = BitmapFactory.decodeByteArray(
                                outputStream.toByteArray(), 0,
                                outputStream.toByteArray().length);
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.v("Error", e.toString());
                    }

                    String filename = Environment.getExternalStorageDirectory().getAbsoluteFile() + File.separator + File.separator
                            + Utils.getCurrentTimeInFormate() + ".png";
                    Log.e("BITMAP", filename);
                    FileOutputStream out = new FileOutputStream(filename);
                    bmp.compress(Bitmap.CompressFormat.PNG, 50, out);

                    Bitmap icon = bmp;

                    ContentValues values = new ContentValues();
                    values.put(MediaStore.Images.Media.TITLE, "title");
                    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
                    Uri uri = context.getContentResolver().insert(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                    OutputStream outstream;
                    try {
                        outstream = context.getContentResolver()
                                .openOutputStream(uri);
                        icon.compress(Bitmap.CompressFormat.PNG, 60, outstream);
                        outstream.close();
                    } catch (Exception e) {
                        System.err.println(e.toString());
                    }
                    share.putExtra(Intent.EXTRA_STREAM, uri);
                }
                context.startActivity(Intent.createChooser(share, "Share"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}