2013-01-14 80 views
0

我正嘗試在Android應用程序的用戶牆上發佈帶有標題的圖像。但它不起作用。它不會在我的牆上顯示任何內容。從Facebook發佈圖片不工作

這裏是我的代碼:

String APP_ID = " My ID"; 
       fb = new Facebook(APP_ID); 
       sp = getPreferences(MODE_PRIVATE); 
       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); 
       } 


btnLogin.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       facebook.authorize(FbdemoActivity.this, 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() { 
        } 
       }); 

       posttowall(); 
      } 
     }); 


public void postToWall() { 
     // post on user's wall. 
     try { 
       if (fb.isSessionValid()) { 
        byte[] data = null; 

        Bitmap bi = BitmapFactory.decodeFile("/sdcard/Asa.jpg"); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
        data = baos.toByteArray(); 


        Bundle params = new Bundle(); 
        params.putString(Facebook.TOKEN, fb.getAccessToken()); 
        params.putString("message", "Test from Android AVD"); 
        params.putString("method", "photos.upload"); 
        params.putByteArray("picture", data); 

        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(fb); 
        mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null); 

       } 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 

    } 


public class SampleUploadListener extends BaseRequestListener { 

     public void onComplete(final String response, final Object state) { 
      try { 
       // process the response here: (executed in background thread) 
       Log.d("Facebook-Example", "Response: " + response.toString()); 
       JSONObject json = Util.parseJson(response); 
       final String src = json.getString("src"); 

       // then post the processed result back to the UI thread 
       // if we do not do this, an runtime exception will be generated 
       // e.g. "CalledFromWrongThreadException: Only the original 
       // thread that created a view hierarchy can touch its views." 

      } catch (JSONException e) { 
       Log.w("Facebook-Example", "JSON Error in response"); 
      } catch (FacebookError e) { 
       Log.w("Facebook-Example", "Facebook Error: " + e.getMessage()); 
      } 
     } 

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

它不顯示任何錯誤,但它也不發佈任何東西。 任何人都可以指出我的代碼中缺少什麼或者什麼是錯的?

+0

'params.putString( 「法」,「photos.upload 「)': - 你在哪裏得到這條線?那是來自舊的API,你確定你沒有使用過時的SDK的舊例子嗎? – Igy

+0

檢查此鏈接: http://stackoverflow.com/questions/5168145/android-post-picture-to-facebook-wall –

+0

我在哪裏可以得到最新的Api? –

回答

0

是被認爲是一個問題,在你的代碼 一個權限丟失..

String[] permissions = { "offline_access", "publish_stream", "user_photos", "publish_checkins", 
     **"photo_upload"** }; 

添加此許可,則檢查一下......

+0

我已經添加了權限,但是仍然沒有發佈任何東西,即使是「消息」 –

+0

您應該檢查HACK Facebook的書籍示例...它在FACKBOOK ANDROID SDK中可用。 – DynamicMind

+0

你有任何鏈接嗎? –