Android:分享图片意图不与Facebook合作?

嗨,我有以下代码来共享图像:

// 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一起使用?

有任何想法吗? 我正在物理设备上调试。

所以我想出了问题所在。

我使用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")); 

现在工作得非常好。

我在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(); } } 

希望这对你有所帮助。

即使您没有使用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)));

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

 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。