我想通过Android Face book SDK发布状态和图片。 虽然我能够登录并发布状态但不能显示图像。 这是我的代码,图像。事件监听器发布图像的工作,但登录后单击它时没有任何反应。
package digitaljalebisharefb.digitaljalebishare.fbshare;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.facebook.login.widget.ProfilePictureView;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.model.SharePhoto;
import com.facebook.share.model.SharePhotoContent;
import com.facebook.share.widget.ShareDialog;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
LoginManager loginManager;
Button share,details,image;
ShareDialog shareDialog;
LoginButton login;
ProfilePictureView profile;
Dialog details_dialog;
TextView details_txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
login = (LoginButton)findViewById(R.id.login_button);
profile = (ProfilePictureView)findViewById(R.id.picture);
shareDialog = new ShareDialog(this);
share = (Button)findViewById(R.id.share);
image = (Button)findViewById(R.id.image);
details = (Button)findViewById(R.id.details);
login.setReadPermissions("public_profile email");
share.setVisibility(View.INVISIBLE);
details.setVisibility(View.INVISIBLE);
image.setVisibility(View.INVISIBLE);
details_dialog = new Dialog(this);
details_dialog.setContentView(R.layout.dialog_details);
details_dialog.setTitle("Details");
details_txt = (TextView)details_dialog.findViewById(R.id.details);
details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
details_dialog.show();
}
});
if(AccessToken.getCurrentAccessToken() != null){
RequestData();
share.setVisibility(View.VISIBLE);
details.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
}
image.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view){
if(AccessToken.getCurrentAccessToken() != null) {
public void onClick (View view){
if(AccessToken.getCurrentAccessToken() != null) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.fb);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
AccessToken accessToken = AccessToken.getCurrentAccessToken();
Bundle postParams = new Bundle();
postParams.putString("message", "naval");
postParams. putByteArray ("source", byteArray);
GraphRequest request = new GraphRequest(accessToken, "me/photos", postParams, HttpMethod.POST, null);
GraphRequestAsyncTask asynTaskGraphRequest = new GraphRequestAsyncTask (request);
asynTaskGraphRequest.execute();
// URL image - code 2 - URL images
/* ShareLinkContent content = new ShareLinkContent.Builder()
.setContentTitle("Hey")
.setContentDescription("Yepiiiiiiiii")
.setContentUrl(Uri.parse("http://p.imgci.com/db/PICTURES/CMS/128400/128483.1.jpg"))
.build();*/
// shareDialog.show(content);
}
}
}
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(AccessToken.getCurrentAccessToken() != null) {
share.setVisibility(View.INVISIBLE);
details.setVisibility(View.INVISIBLE);
image.setVisibility(View.INVISIBLE);
profile.setProfileId(null);
}
}
});
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShareLinkContent content = new ShareLinkContent.Builder().build();
shareDialog.show(content);
}
});
login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
if(AccessToken.getCurrentAccessToken() != null){
RequestData();
share.setVisibility(View.VISIBLE);
details.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
public void RequestData(){
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
JSONObject json = response.getJSONObject();
try {
if(json != null){
String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
details_txt.setText(Html.fromHtml(text));
profile.setProfileId(json.getString("id"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
request.executeAsync();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
答案 0 :(得分:0)
您可以使用AsyncRunner类在Facebook上发布和图像/照片。
public void publishPhoto(byte[] imgData, string message) {
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner (facebook);
Bundle parameters = new Bundle ();
parameters.putString (Facebook.TOKEN, facebook.getAccessToken());
parameters.putString ("message", message);
parameters.putByteArray ("source", imgData);
mAsyncRunner.Request ("me/photos", parameters, "POST", new RequestListener (), null);
}
在sharephoto上点击此方法。
更新1
从最新的SDK使用GraphRequestAsyncTask
Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
AccessToken accessToken = AccessToken.getCurrentAccessToken();
Bundle postParams = new Bundle();
postParams.putString("message", msg);
postParams. putByteArray ("source", imgData);
GraphRequest request = new GraphRequest(accessToken, "me/photos", postParams, HttpMethod.POST, null);
GraphRequestAsyncTask asynTaskGraphRequest = new GraphRequestAsyncTask (request);
asynTaskGraphRequest.execute();
P.S假设您拥有所有必需的许可。
答案 1 :(得分:0)
这是我找到的解决方案:
如果您想在登录前在墙上发布您需要这样做的图像:
private static List<String> PERMISSIONS =
Arrays.asList("public_profile","user_photos","user_videos", "email","user_likes","user_posts",
"user_hometown", "user_location","user_about_me","user_birthday",
"user_friends","user_relationship_details");
LoginManager.getInstance().logInWithPublishPermissions(this,Arrays.asList("publish_actions"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.i(TAG,"onSuccess registerCallback");
}
@Override
public void onCancel() {
// App code
Log.i(TAG,"onCancel registerCallback");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.i(TAG,"onError registerCallback");
}
});
然后你需要调用这个方法:
/**
* This is a method which can post an image to the facebook wall.
* @throws IOException
*/
private void sharePhoto() throws IOException {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
String imageUri = "drawable://" + R.drawable.gift_icon;
/*OR*/
Uri path = Uri.parse("android.resource://dxswifi_direct.com.wifidirectcommunication/" + R.drawable.gift_icon);
Uri otherPath = Uri.parse("android.resource://dxswifi_direct.com.wifidirectcommunication/drawable/gift_icon");
String path1 = path.toString();
String path2 = otherPath.toString();
imageUri = "http://i.dailymail.co.uk/i/pix/2015/04/22/21/27DEB82000000578-3051045-image-a-25_1429735533586.jpg";
URL url = new URL(imageUri);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
/*SharePhoto photo = new SharePhoto.Builder().setBitmap(image).build();
Bitmap image = BitmapFactory.decodeFile(imageUri, bmOptions);*/
SharePhoto photo = new SharePhoto.Builder().setBitmap(image).build();
SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
Toast.makeText(getApplicationContext(), getString(R.string.facebook_uploading), Toast.LENGTH_SHORT).show();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result)
{
Toast.makeText(getApplicationContext(), getString(R.string.facebook_successful), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel()
{
Log.v("FACEBOOK_TEST", "share api cancel");
}
@Override
public void onError(FacebookException e)
{
Log.v("FACEBOOK_TEST", "share api error " + e);
}
});
}