如何从绘制文件夹上传图像与图形API向Facebook? [英] How to post image from drawable folder to facebook with graph api?
问题描述
我想用自己的图形API张贴到Facebook的图像。但我的形象是在绘制文件夹中。到目前为止:
I'd like to post an image to facebook by using their graph api. But my image is in drawable folder. So far:
public static void postToFacebook() {
Bundle params = new Bundle();
// shared with this
params.putString("caption", activityContext.getString(R.string.app_name));
// description of the program
params.putString("description", "bla bla bla bla bla bla");
// header of the description
params.putString("name", "train");
// callback url
params.putString("link","http://www.google.com.tr");
// put a personal message
params.putString("message", "my message");
// to put a photo
params.putByteArray("picture", getImageAsData());
try {
String response = FacebookUtility.mFacebook.request("/me/feed",
params, "POST");
if (response == null || response.equals("")
|| response.equals("false"))
Toast.makeText(activityContext, "Error.",
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] getImageAsData() {
Drawable d = activityContext.getResources().getDrawable(R.drawable.icon);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
return bitmapdata;
}
但是,这是不工作...这不是我发帖的图标,但它会从网站获得谷歌图标。我尝试注释掉的链接部分,但这次没有发送任何东西,但消息。
But this is not working... It's not posting my icon, but it gets google icon from site. I try to comment out link part, but this time it didn't send anything but message.
我也试图得到位图:
Bitmap bitmap = BitmapFactory.decodeResource(activityContext.getResources(), R.drawable.icon);
但是,这也没有发挥出来。
But this also didn't work out.
我见过的:
Bitmap bi = BitmapFactory.decodeFile(pathonsdcard);
但我不能用这个方法来获取位图,因为我没有pathonsdcard?
But i couldn't use this method to get bitmap, because i have no pathonsdcard?
任何建议?
所以,我怎么可以发布它位于RES /文件夹的绘制形象?
先谢谢了。
Any suggestion? So how can i post image which is located in res/drawable folder ? Thanks in advance.
推荐答案
这是上传从绘制照片的正确方法。需要注意的是你唯一需要的参数是照片和标题。
This is the correct way to upload a photo from a drawable. Note that the only parameters you need is "photo" and "caption".
Bundle params = new Bundle();
params.putByteArray("photo", getImageAsData());
params.putString("caption", "Test caption lololol");
mAsyncRunner.request("me/photos", params, "POST", new PhotoUploadListener(), null);
这篇关于如何从绘制文件夹上传图像与图形API向Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!