安卓:分享图片意图与Facebook不兼容?

3

你好,我有以下代码想分享一张图片:

// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");

Uri uri = Uri.parse(getFilesDir() + File.separator + "myGoal.jpg");
        share.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(share, "Share Image"));

分享图片到Dropbox是有效的,但如果我选择Facebook选项,会弹出Facebook的状态更新对话框,但没有附带图片,如果我尝试使用"测试"来更新我的状态,则无法工作。没有错误,只是不起作用。
我知道这不是图片的问题,因为它已经成功上传到我的Dropbox,我可以打开图片查看它。
我需要以不同的方式将图片附加到意图中才能让它在Facebook上起作用吗?
有任何想法吗?我正在物理设备上进行调试。
4个回答

10

所以,我找到了问题所在。

我是通过getFilesDir()将图片保存到内部存储中,这使得图片只能被我的应用程序访问而无法被其他应用程序访问。

我用下面的代码替换了原来的代码:

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                "/MyApp/";

File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "myPic.png");
FileOutputStream fOut = new FileOutputStream(file);

screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);

fOut.flush();
fOut.close();

// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");

share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "My Image");

startActivity(Intent.createChooser(share, "Share Image"));

现在完美地运行。


你的 EXTRA_TEXT 是否真正出现在你的 Facebook 帖子中?据我所知,它从未被响应 Facebook ACTION_SEND Intent 的 Activity 使用。 - William
3
如果有人需要帮助,我认为让它起作用的实际方法是从 share.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath)); 更改为 share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));干杯! - Olaia
实际上,我认为这可能是位置更改和传递额外方式的组合。至少我总是有外部路径,但只使用Uri.fromFile - Olaia
你的代码中的screenshot是什么?如何初始化它? - hasnain_ahmad
如果我没记错的话,截图是一个位图对象。如果你跟着代码走,你会看到我创建了一个文件对象,并将位图数据以PNG格式推送到它上面。然后同样的文件被用作Intent的一部分。 - ReX357
有没有办法使用应用程序内部存储getFilesDir()中存储的文件来执行Intent.ACTION_SEND操作? - Hiren Dabhi

0
我在Facebook上发布图片所做的是,不直接传递它,而是创建一个函数并将其编写如下:
 private void ShareWall(String message) {
        Bundle parameters = new Bundle();
                // share msg
        parameters.putString("message", message);
                // shre image which you have define above
        parameters.putString("picture", postImage);

        try {
            facebook.request("me");
            String response = facebook.request("me/feed", parameters, "POST");
            Log.d("response: ", response);
            if (response == null || response.equals("")) {
                response.equals("");
                showToast("No Response.");

            } else {
                showToast("Message has been posted to your walll!.");
            }
            finish();
        } catch (Exception e) {
            showToast("Message failed to posdt on wall.");
            e.printStackTrace();
            finish();
        }

    }

希望这能帮助到你。

0

即使您没有使用Facebook SDK,仍然可以从您的应用程序向Facebook分享图像(但不是文本)。

只需确保使用Uri.fromFile而不是Uri.parse,它就会起作用:

请勿使用: intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathToFile));

使用: intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pathToFile)));


0

这里有一个不使用外部文件写入的解决方案:

Drawable mDrawable = myImageView1.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));

在这种情况下,我的图像来自于一个ImageView myImageView。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接