2015-06-15 47 views
2

我正在制作一个Android应用程序和这个应用程序,我需要分享一个图像到Facebook。我已经按照从Facebook分享网站的所有程序。但是,点击分享按钮时,它会显示图像,但分享图像时缺失。如果您正在使用Facebook的SDK版本4 比使用此代码与文本共享图片我的详细代码是 -附加图片是misssing分享到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

我为此需要一个解决方案。 – ktina51

+0

从代码看,它看起来不像官方的Facebook Android SDK--你使用的是哪个SDK?您是否尝试过使用从Facebook提供的官方SDK代码片段(https://developers.facebook.com/docs/sharing/android)? – subeeshb

+0

谢谢@subeeshb。我解决了我的问题。现在我可以将图像分享到Facebook。 – ktina51

回答

0

。这对我很有用。

1.declaire变量

private LoginManager manager;
2.put这的onCreate并更改布局名称
FacebookSdk.sdkInitialize(this.getApplicationContext()); 
 
setContentView(R.layout.Your_layout); 
 
callbackManager = CallbackManager.Factory.create();
3.call这种方法分享
public void FaceBookSharing() { 
 
\t List<String> permissionNeeds = Arrays.asList("publish_actions"); 
 
\t manager = LoginManager.getInstance(); 
 
\t manager.logInWithPublishPermissions(this, permissionNeeds); 
 
\t manager.registerCallback(callbackManager, 
 
\t new FacebookCallback<LoginResult>() { 
 
\t @Override 
 
\t public void onSuccess(LoginResult loginResult) { 
 
\t shareImage(); 
 
\t } 
 
\t @Override 
 
\t public void onCancel() { 
 
\t System.out.println("onCancel"); 
 
\t } 
 
\t @Override 
 
\t public void onError(FacebookException exception) { 
 
\t System.out.println("onError"); 
 
\t } 
 
}); 
 
} 
 

 
private void shareImage() { 
 
//use your image bitmap here 
 
\t Bitmap image = BitmapFactory.decodeResource(getResources(), 
 
\t \t \t \t R.drawable.ic_launcher); 
 
\t SharePhoto photo = new SharePhoto.Builder().setBitmap(image) 
 
\t \t \t \t .setCaption("Hey this is only demo text.!") 
 
\t \t \t \t .build(); 
 
\t SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(
 
\t \t \t \t photo).build(); 
 
\t ShareApi.share(content, null); 
 
\t \t Toast.makeText(this, "Succsesfully posted on your wall", Toast.LENGTH_LONG) 
 
\t \t .show(); \t 
 
}