Java 安卓:共享图像意图不与Facebook合作?
嗨,我有以下代码来共享图像:Java 安卓:共享图像意图不与Facebook合作?,java,android,facebook,android-intent,Java,Android,Facebook,Android Intent,嗨,我有以下代码来共享图像: // 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.createCho
// 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一起使用
有什么想法吗?我正在一台物理设备上调试。我在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();
}
}
希望这对你有帮助。所以我解决了这个问题 我用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 SDK,您仍然可以将应用程序中的图像(但不是文本)共享到Facebook 只需确保使用Uri.fromFile而不是Uri.parse即可: 不要使用: intent.putExtra(intent.EXTRA_流,Uri.parse(pathToFile)) 使用:
intent.putExtra(intent.EXTRA_流,Uri.fromFile(新文件(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。您的额外文本是否实际出现在您的facebook帖子中?据我所知,它从未被响应Facebook ACTION_SEND Intent的活动所使用。如果它对某人有帮助,我认为它真正起作用的是从
share.putExtra(Intent.EXTRA_STREAM,Uri.parse(filePath))
toshare.putExtra(Intent.EXTRA_流,Uri.fromFile(新文件(文件路径))代码>干杯!事实上,我认为这可能是位置变化和传递额外信息的方式的结合。至少我总是有外部路径,但只使用Uri.fromFile
代码中的屏幕截图是什么?如何初始化它?@hasnain_ahmad如果我没记错的话,截图是一个位图对象。如果您按照代码进行操作,您会看到我创建了一个文件对象,并将位图数据以PNG格式推送到其中。然后,相同的文件被用作意图的一部分。