2015-06-15 75 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 
 
}