1

我必须与所有社交媒体分享带有文本的图像。所以我尝试了下面的代码: -

 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。

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

通过使用“文本/纯文本”作为 shareIntent 类型 Facebook 出现但无法共享图像...

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

提前致谢。

4

2 回答 2

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您必须传递您必须在其中发布图像的应用程序的名称。在你的情况下 pass facebook

于 2016-11-18T09:38:13.923 回答
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();
}
于 2016-11-18T10:01:03.747 回答