Android的 - 如何照片从SD卡上传到Facebook的墙 [英] Android - How to upload photo from the SD card to the Facebook wall
问题描述
我使用Facebook的Android SDK。
目标
在Facebook的新闻提要创建多个职位登录的用户将包含照片从Android设备(其 SD卡)和一些评论。其结果应该是一样的,当你使用添加照片/视频功能,直接在Facebook的作为。最后,它应该是这样的:
求购Facebook的结果
问题
我不能这样做。
我通过堆栈溢出的大量类似的帖子去了,但没有答案有这么远。</ P>
我试图实施至今
方法1:SD卡中的照片2 Facebook相册
如何
上传从我的手机(它的SD卡)的照片,为我的应用程序创建我第一次上传图片从它的一张专辑。在这种情况下,构建params对象的时候,我用的是图片
键,把画面的字节数作为其值。我用我/照片
在请求(...)Facebook的(或AsyncFacebookRunner)对象
通话。 **
问题
并非所有上传的图片显示在我的墙上。相反,也有一些是像 X照片被添加到相册XXX 的。
的code片段是这样的(一个图片)
捆绑PARAMS =新包();
params.putString(信息,上传于+ NOW());
params.putByteArray(图片报,字节); //字节包含照片的字节,这里没有问题
asyncRunner.request(ME /照片,PARAMS,POST,新PostPhotoRequestListener(),NULL);
Facebook的结果
方法二:互联网照片2张的Facebook新闻源
如何
显示图片的地方存放在互联网上的挂在墙上的职位。在这种情况下,构建params对象的时候,我用的是链接
键,设置URL以图片作为其值。我用 ME /进料
在请求(...)
电话。
问题
这会产生一些奇怪的输出,但它不是我想要的。
的code片段是这样的(一个图片)
捆绑PARAMS =新包();
params.putString(信息,上传于+ NOW());
params.putString(链接,http://i1114.photobucket.com/albums/k538/tom_rada/bota2.jpg);
asyncRunner.request(ME /饲料,PARAMS,POST,新PostPhotoRequestListener(),NULL);
Facebook的结果
方法3:方法1和2
混合如何
我尝试使用图片
键,设置照片的字节的是它的值(如1),并调用与<请求code> ME /进料(如2),
问题
信息是作为我希望它是,但没有照片包含
的code片段是这样的(一个图片)
捆绑PARAMS =新包();
params.putString(信息,上传于+ NOW());
params.putByteArray(图片报,字节); //字节包含照片的字节,这里没有问题
asyncRunner.request(ME /饲料,PARAMS,POST,新PostPhotoRequestListener(),NULL);
Facebook的结果
因此,任何想法如何,我可以实现我的目标是什么?
修改 - 替代方法FOUND
看来,只有这样,才能创建一个包含用户的墙上照片的新职位是添加的照片和相关评论,以用户的墙照片的专辑。
如何 - code段
请注意:该 facebook.request
调用应该与异步调用所取代,所以操作不会阻塞UI线程!
字符串wallAlbumID = NULL;
字符串响应= facebook.request(ME /相册);
JSONObject的JSON = Util.parseJson(响应);
JSONArray专辑= json.getJSONArray(数据);
的for(int i = 0; I&LT; albums.length();我++){
JSONObject的专辑= albums.getJSONObject(我);
如果(album.getString(类型)。equalsIgnoreCase(墙)){
wallAlbumID = album.getString(ID);
Log.d(JSON,wallAlbumID);
打破;
}
}
...然后
如果(wallAlbumID!= NULL){
捆绑PARAMS =新包();
params.putString(信息,上传于+ NOW());
params.putByteArray(源,字节);
asyncRunner.request(wallAlbumID +/照片,PARAMS,POST,新PostPhotoRequestListener(),NULL);
}
商标=新的Facebook(你的appid);
私人无效uploadImage()
{
BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream流=新ByteArrayOutputStream();
bmp.com preSS(Bitmap.Com pressFormat.PNG,100,流);
最后一个字节[]数据= stream.toByteArray();
facebook.authorize(FacebookActivity.this,新的String [] {user_photos,publish_checkins,publish_actions,publish_stream},新DialogListener()
{
@覆盖
公共无效的onComplete(束值)
{
// uploadImageOnlyToWall(数据,上载图片仅墙,测试后,从Android的同时,上传照片与消息);
uploadImageToWallAndAlbums(IMAGEURL,通过链接图像);
}
@覆盖
公共无效onFacebookError(FacebookError错误)
{
Toast.makeText(FacebookActivity.this,脸谱错误,Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onerror的(DialogError E)
{
Toast.makeText(FacebookActivity.this,错误,Toast.LENGTH_LONG).show();
}
@覆盖
公共无效OnCancel的()
{
Toast.makeText(FacebookActivity.this,取消,Toast.LENGTH_LONG).show();
}
});
}
私人无效uploadImageOnlyToAlbum(byte []的字节数组,字符串字幕)
{
捆绑PARAMS =新包();
params.putByteArray(图片报,字节);
params.putString(标题,字幕);
AsyncFacebookRunner mAsyncRunner =新AsyncFacebookRunner(脸谱);
mAsyncRunner.request(ME /照片,PARAMS,POST,新SampleUploadListener(),NULL);
}
私人无效uploadImageToWallAndAlbums(byte []的字节数组,字符串字幕)
{
捆绑PARAMS =新包();
params.putString(法,photos.upload);
params.putByteArray(图片报,字节);
params.putString(标题,字幕);
AsyncFacebookRunner mAsyncRunner =新AsyncFacebookRunner(脸谱);
mAsyncRunner.request(NULL,则params,POST,新SampleUploadListener(),NULL);
}
I use the Facebook Android SDK.
Goal
Create multiple posts in news feed of Facebook logged in user that will contain photo from the Android device (its SD card) and some comment. The result should be the same as when you do it using the Add photo/video feature directly in Facebook. In the end, it should look like this:
Wanted Facebook result
Problem
I can't do it.
I went through the numerous similar posts on Stack Overflow, but no answer there so far.
What I have tried to implement so far
Approach 1: SD card photos 2 Facebook album
How
Upload pictures from my mobile (its SD card) to an album that is created for my application the first time I upload a picture from it. In this case, when constructing the params object, I use the picture
key and put the bytes of the picture as its value. I use me/photos
in the request(...)
call of the Facebook (or AsyncFacebookRunner) object. **
The problem
Not all uploaded images are displayed on my wall. Instead, there is something like x photos were added to the album xxx.
The code snippet is this (for one picture)
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);
Facebook result
Approach 2: Internet photos 2 facebook news feed
How
Display pictures stored somewhere on the Internet in posts on my wall. In this case, when constructing the params object, I use the link
key and set the url to picture as its value. I use me/feed
in the request(...)
call.
The problem
This produces some strange output, but it isn't what I want.
The code snippet is this (for one picture)
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);
Facebook result
Approach 3: Mix of approach 1 and 2
How
I try to use the picture
key and set photo bytes as its value (as in 1.), and call the request with me/feed
(as in 2.),
The problem
Message is produced as I would like it to be, but no photo is included
The code snippet is this (for one picture)
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);
Facebook result
So, any ideas how I could reach my goal?
EDIT - WORKAROUND FOUND
It seems that the only way to create new posts containing photos on user's wall is to add photos and related comments to user's Wall photos album.
How - Code snippet
Beware: The facebook.request
call should be replaced with async call, so the operation doesn't block the UI thread !!!
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;
}
}
... and then
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);
}
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);
}
这篇关于Android的 - 如何照片从SD卡上传到Facebook的墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!