我正在制作一个Android应用程序,对于这个应用程序,我需要与Facebook分享图像。我已经按照Facebook分享网站的所有程序。但是,单击共享按钮时,它会显示图像,但共享图像时却丢失了。我的详细代码是 -
public class ActivityShare extends Activity {
String[] ShareOption; //create a shareOption array
Spinner ShareOptionList; // define A spinner list to choose the score type
int driverStarScore = 4; // define a drivingstar score to give the star to a driver .
Facebook fb; // define facebook
SharedPreferences sp;
ImageView pic;
ListView list; // define list
String[] ShareItemName ={ //create a string array of shareItem name
"Dropbox",
"Email",
"Facebook",
"Google Plus",
"Twitter",
"Whatsapp",
};
Integer[] ShareImageId={ //create an integer array of images of the shareitem
R.drawable.ic_dropbox,
R.drawable.ic_email,
R.drawable.ic_facebook,
R.drawable.ic_googleplus,
R.drawable.ic_twitter,
R.drawable.ic_whatsapp,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
sp=getPreferences(MODE_PRIVATE);
String access_token= sp.getString("access_token", null);
long expires=sp.getLong("access_expires", 0);
if(access_token!=null){
fb.setAccessToken(access_token);
}
if(expires !=0){
fb.setAccessExpires(expires);
}
String APP_ID=getString(R.string.APP_ID);
fb=new Facebook(APP_ID);
ShareOptionList=(Spinner) findViewById(R.id.spinner_ShareScore);
ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ShareOption);
ShareOptionList.setAdapter(adapter1);
ShareOptionList.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int index=arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You select "+ ShareOption[index],Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
ShareListAdapter adapter=new ShareListAdapter(this, ShareItemName, ShareImageId);
list=(ListView)findViewById(R.id.listview_share);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Selecteditem= ShareItemName[+position]; //response to click on the list
Toast.makeText(ActivityShare.this, Selecteditem, Toast.LENGTH_SHORT).show();
if(Selecteditem=="Facebook"){ //if facebook item is pressed
Bundle params=new Bundle();
params.putString("name", "ASSES ME");
params.putString("caption", "My Driving Score");
params.putString("link", "http://www.mybringback.com/"); //the link of the app
if (driverStarScore == 1){ // if someone got 1 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/944/1star.png"); //image copied from a Website
params.putString("description", "My score is Sufficient"); //summary of a driver
}
else if (driverStarScore == 2){ // if someone got 2 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/946/2star.png");//image copied from a Website
params.putString("description", "My score is Satisfactory");//summary of a driver
}
else if (driverStarScore == 3){ // if someone got 3 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/948/3star.png");
params.putString("description", "My score is Good");
}
else if (driverStarScore == 4){
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/950/4star.png");
params.putString("description", "My score is Really Good");
}
else if (driverStarScore == 5){
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/951/5star.png");
params.putString("description", "My score is Best");
}
fb.dialog(ActivityShare.this,"feed",params, new DialogListener(){
@Override
public void onFacebookError(FacebookError e){
//TODO Auto-generated method stub
}
@Override
public void onError(DialogError e){
//TODO Auto-generated method stub
}
@Override
public void onCancel(){
//TODO Auto-generated method stub
}
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor editor=sp.edit();
editor.putString("access_token",fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
}
});
}
答案 0 :(得分:0)
如果您使用的是facebook sdk版本4 而不是使用此代码与文本共享图像。这对我有用。
1.declaire变量
private LoginManager manager;
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.Your_layout);
callbackManager = CallbackManager.Factory.create();
public void FaceBookSharing() {
List<String> permissionNeeds = Arrays.asList("publish_actions");
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(this, permissionNeeds);
manager.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
shareImage();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
}
});
}
private void shareImage() {
//use your image bitmap here
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
.setCaption("Hey this is only demo text.!")
.build();
SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(
photo).build();
ShareApi.share(content, null);
Toast.makeText(this, "Succsesfully posted on your wall", Toast.LENGTH_LONG)
.show();
}