如何发布图像从Android应用至Facebook
问题描述:
可能重复:结果
的Android后图片的Facebook墙上
我是新来Facebook的Android SDK中。我已经加入了SDK到Eclipse作为Android项目。在我的应用程序有一个形象和一个按钮。我希望图像上传到Facebook的按钮时是pressed。我在阅读SO各个岗位。但我无法弄清楚如何去做。任何帮助吗?
I am new to facebook android sdk. I have added the sdk to eclipse as android project. In my app there is an image and a button. I want the image to be uploaded to facebook when button is pressed. I have read various posts on SO. But I am not able to figure out how to go about it. Any help ?
答
这个尝试,
Button mButton=(Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Facebook mFacebook=new Facebook(yourAppID)
byte[] data = null;
Bitmap bi = BitmapFactory.decodeFile(imageLink);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
mAsyncRunner.request(null, params, "POST",
new SampleUploadListener(), null);
}
});
public class SampleUploadListener extends BaseRequestListener {
@SuppressWarnings("unused")
public void onComplete(final String response, final Object state) {
try {
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
String src = json.getString("src");
PublishImage.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"Successfully Uploaded", Toast.LENGTH_SHORT)
.show();
}
});
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
public abstract class BaseRequestListener implements RequestListener {
public void onFacebookError(FacebookError e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onFileNotFoundException(FileNotFoundException e,
final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onIOException(IOException e, final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
public void onMalformedURLException(MalformedURLException e,
final Object state) {
Log.e("Facebook", e.getMessage());
e.printStackTrace();
}
}