我使用Facebook Android SDK.
在Facebook登录用户的新闻Feed中创建多个帖子,其中包含来自Android设备(其SD卡)的照片和一些评论.结果应与使用Facebook直接添加照片/视频功能时的结果相同.最后,它应该是这样的:
我不能这样做.
我浏览了Stack Overflow上的众多类似帖子,但到目前为止还没有答案.
将图片从我的手机(SD卡)上传到我第一次上传图片时为我的应用程序创建的相册.在这种情况下,在构造params对象时,我使用picture密钥并将图片的字节作为其值.我me/photos在request(...)Facebook(或AsyncFacebookRunner)对象的调用中使用.**
并非所有上传的图像都显示在我的墙上.相反,有一些像x照片被添加到专辑xxx.
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/photos", params, "POST", new PostPhotoRequestListener(), null);
Run Code Online (Sandbox Code Playgroud)
在我的墙上的帖子中显示存储在互联网上某处的图片.在这种情况下,在构造params对象时,我使用link键并将url设置为picture作为其值.我me/feed在request(...)通话中使用.
这会产生一些奇怪的输出,但它不是我想要的.
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putString("link", "http://i1114.photobucket.com/albums/k538/tom_rada/bota2.jpg");
asyncRunner.request("me/feed", params, "POST", new PostPhotoRequestListener(), null);
Run Code Online (Sandbox Code Playgroud)
我尝试使用picture密钥并将照片字节设置为其值(如在1.中),并使用me/feed(如在2.中)调用请求,
消息按照我的意愿生成,但不包含照片
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/feed", params, "POST", new PostPhotoRequestListener(), null);
Run Code Online (Sandbox Code Playgroud)
似乎在用户墙上创建包含照片的新帖子的唯一方法是向用户的Wall相册添加照片和相关评论.
注意:facebook.request应该用异步调用替换调用,因此操作不会阻止UI线程!
String wallAlbumID = null;
String response = facebook.request("me/albums");
JSONObject json = Util.parseJson(response);
JSONArray albums = json.getJSONArray("data");
for (int i =0; i < albums.length(); i++) {
JSONObject album = albums.getJSONObject(i);
if (album.getString("type").equalsIgnoreCase("wall")) {
wallAlbumID = album.getString("id");
Log.d("JSON", wallAlbumID);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
... 然后
if (wallAlbumID != null) {
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("source", bytes);
asyncRunner.request(wallAlbumID+"/photos", params, "POST", new PostPhotoRequestListener(), null);
}
Run Code Online (Sandbox Code Playgroud)
Facebook facebook = new Facebook("your appid");
private void uploadImage()
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] data = stream.toByteArray();
facebook.authorize(FacebookActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener()
{
@Override
public void onComplete(Bundle values)
{
//uploadImageOnlyToWall(data, "Uploading Image only to wall","Test Post from Android while uploading photo with message");
uploadImageToWallAndAlbums(imageUrl, "Image via link");
}
@Override
public void onFacebookError(FacebookError error)
{
Toast.makeText(FacebookActivity.this, "FaceBook Error", Toast.LENGTH_LONG).show();
}
@Override
public void onError(DialogError e)
{
Toast.makeText(FacebookActivity.this, "Error", Toast.LENGTH_LONG).show();
}
@Override
public void onCancel()
{
Toast.makeText(FacebookActivity.this, "Canceled", Toast.LENGTH_LONG).show();
}
});
}
private void uploadImageOnlyToAlbum(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putByteArray("picture", byteArray);
params.putString("caption",caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}
private void uploadImageToWallAndAlbums(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", byteArray);
params.putString("caption", caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7149 次 |
| 最近记录: |