2013-11-23 107 views
0

我正在嘗試創建一個Android應用程序,該應用程序會自動在用戶的Facebook牆上發送圖像。我已經在網上閱讀了很多例子(也來自這個網站),但顯然沒有任何東西可以用於我。這是我的代碼:Android:在Facebook上發佈圖像

class FacebookConnection extends AsyncTask<String, String, String> { 

    private Exception exception; 
    private Facebook facebook; 
    private static String APP_ID = "574586632609202"; // Replace your App ID here 
    Activity activity; 

    public FacebookConnection(Activity activity) 
    { 
      this.activity = activity; 
    } 

    private void login() 
    { 
     facebook = new Facebook(APP_ID); 
     facebook.authorize(activity, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"}, 
       new DialogListener() { 
      @Override 
      public void onComplete(Bundle values) { 
      } 

      @Override 
      public void onFacebookError(FacebookError error) { 
      } 

      @Override 
      public void onError(DialogError e) { 
      } 

      @Override 
      public void onCancel() { 
      } 
     }); 
    } 

    protected String doInBackground(String... path) { 
     login(); 
     Bundle parameters = new Bundle(); 
     parameters.putString("message", "ciao"); 

     Bitmap bi = BitmapFactory.decodeFile(path[0]); 
     AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bi.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 
     parameters.putByteArray("picture", byteArray); 

     String response = null; 
     try { 
       facebook.request("me"); 
      response = facebook.request("me/feed", parameters, "POST"); 
      } catch (MalformedURLException e) { 
       return e.getMessage(); 
      } catch (IOException e) { 
       return e.getMessage(); 
      } 

     if (response == null || response.equals("")) 
      return "No Response.."; 
     else 
      return "Message has been posted to your walll!"; 
    } 

    protected void onPostExecute(String mess) { 
     show("DONE", mess); 
    } 
} 

NB:我使用的按鈕點擊的主要活動是一流的,我通過我的主要活動作爲參數。方法「show」只是在消息框中寫入作爲參數傳遞的字符串。 看來,登錄是好的,但是當我檢查我的臉譜牆,沒有什麼改變。 謝謝你的幫助!

+0

是的,我使用的AsyncTask避免NetworkOnMainThreadException ...我還沒有插入onPreExecute ......但doInBackground和onPostExecute在類裏面我已經貼!沒有? – user3025616

+0

哦,我沒有看到你的代碼的其餘部分!咄! – hichris123

+0

沒問題! ; ) 任何想法? – user3025616

回答

0

求解: 我已經解決了我的問題!以下是完整的代碼:

public class FacebookConnector { 
    Facebook facebook; 
    Context context; 
    Activity activity; 
    Handler mHandler; 
    String[] permissions; 
    String path; 

    public FacebookConnector(String appId, Activity activity, String[] permissions) { 
     this.facebook = new Facebook(appId); 
     this.activity = activity; 
     this.permissions=permissions; 
     this.mHandler = new Handler(); 
    } 

    public void setImagePath(String path) { 
     this.path = path; 
    } 

    public void login() { 
     facebook.authorize(this.activity, this.permissions, Facebook.FORCE_DIALOG_AUTH, new DialogListener() { 
      @Override 
      public void onCancel() { 
       Toast.makeText(activity, "Canceled", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onComplete(Bundle values) { 
       postImageonWall(); 
       Toast.makeText(activity, "Image Posted on Facebook.", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onError(DialogError error) { 
       Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onFacebookError(FacebookError fberror) { 
       Toast.makeText(activity, "Facebook Error", Toast.LENGTH_SHORT).show(); 

      } 

     });} 

    public void postImageonWall() {    
     byte[] data = null;    

     Bitmap bi = BitmapFactory.decodeFile(path); 
     //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon);    
     ByteArrayOutputStream baos = new ByteArrayOutputStream();    
     bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);    
     data = baos.toByteArray();     
     Bundle params = new Bundle();    
     params.putString(Facebook.TOKEN, facebook.getAccessToken());    
     params.putString("method", "photos.upload");    
     params.putByteArray("picture", data);    
     AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);    
     mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);  
    } 

    public void execute() { 
     login(); 
    } 

    class SampleUploadListener implements AsyncFacebookRunner.RequestListener{ 

     @Override 
     public void onComplete(String response, Object state) { 
     } 

     @Override 
     public void onIOException(IOException e, Object state) { 
     } 

     @Override 
     public void onFileNotFoundException(FileNotFoundException e, 
       Object state) { 
     } 

     @Override 
     public void onMalformedURLException(MalformedURLException e, Object state) { 
     } 

     @Override 
     public void onFacebookError(FacebookError e, Object state) { 
     } 

    } 
} 

當然,您必須在調用execute之前調用setImagePath方法! 問候

弗朗西斯