2013-06-02 71 views
5

對此似乎沒有明確的參考。 我正在創建一個Android應用程序,用戶可以登錄到FB。Android Facebook SDK 3.0上傳本地圖片

我跟着this tutorial on FB site,它給出了一個從網址發佈圖片的例子: postParams.putString(「picture」,「https:// image URL」);

但是,我想上傳到登錄用戶的時間線上從我的項目,位於所有res drawable文件夾的本地PNG圖像。

這裏是我的代碼:

void publishStory() 
{ 
     Session session = Session.getActiveSession(); 

     if (session != null) 
     {  
      Bundle postParams = new Bundle(); 

      postParams.putString("name", "Name here."); 
      postParams.putString("caption", "Caption here."); 
      postParams.putString("description", "Description here."); 
      postParams.putString("link", "https://developers.facebook.com/android"); 

      byte[] data = null; 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      Bitmap bi = BitmapFactory.decodeResource(getResources(),R.drawable.logonew); 
      bi.compress(Bitmap.CompressFormat.PNG, 100, baos); 
      data = baos.toByteArray(); 

      postParams.putString("method", "photos.upload"); 
      postParams.putByteArray("picture", data); 

      Request.Callback callback = new Request.Callback() 
      { 
       public void onCompleted(Response response) 
       { 
        FacebookRequestError error = response.getError(); 

        if (error != null) 
         Toast.makeText(_context , error.getErrorMessage(), Toast.LENGTH_SHORT).show(); 

        else 
         Toast.makeText(_context, "Posted successful on your wall", Toast.LENGTH_SHORT).show(); 
       } 
      }; 

      Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback); 
      RequestAsyncTask task = new RequestAsyncTask(request); 
      task.execute(); 
     } 
    } 

所有的例子我能找到正在處理與Facebook類實例和AsyncFacebookRunner這是鬱悶。

此外,錯誤響應我從請求得到的是: 的HTTPStatus:400,錯誤碼:100,ERRORTYPE:GraphMethodException,的errorMessage:不支持的方法,photos.upload

那麼什麼是photos.upload替代?請指教,代碼示例會很棒,tnx。

+0

你見過我提供的答案嗎?如果它對你有用,如果你接受它會很好。 – PeteH

回答

7

如果您想上傳照片,爲什麼不直接在Reqeust類中使用newUploadPhotoRequest? https://developers.facebook.com/docs/reference/android/3.0/Request#newUploadPhotoRequest%28Session,%20Bitmap,%20Callback%29

Bitmap image = ... // get your bitmap somehow 
Request.Callback callback = ... // create your callback 
Request request = Request.newUploadPhotoRequest(session, image, callback); 
request.executeAsync(); 
+1

這是一個好的開始。但是,您如何從回調中獲取上傳圖片的網址? Facebook的文檔沒有解釋如何做到這一點。 – PeteH

+0

回調給你一個Response對象,並且從Response對象中,你可以得到一個GraphObject,它實際上只是一個json響應的代理。你在響應中沒有得到一個url,你得到了被創建的圖像的id。 –

+1

謝謝@MingLi李 –

4

Ming Li讓我走上了正軌,但這裏有一個更完整的解決方案。這是測試和工作。有兩個要素:(1)創建回調來獲取上傳的照片的URL; (2)代碼上傳照片。這是兩部分的完整代碼。照片的URL將被加載到字符串變量fbPhotoAddress中。

String fbPhotoAddress = null; 

// Part 1: create callback to get URL of uploaded photo 
    Request.Callback uploadPhotoRequestCallback = new Request.Callback() { 
    @Override 
     public void onCompleted(Response response) { 
    // safety check 
      if (isFinishing()) { 
      return; 
     } 
     if (response.getError() != null) { // [IF Failed Posting] 
      Log.d(logtag, "photo upload problem. Error="+response.getError()); 
     } // [ENDIF Failed Posting] 

     Object graphResponse = response.getGraphObject().getProperty("id"); 
     if (graphResponse == null || !(graphResponse instanceof String) || 
      TextUtils.isEmpty((String) graphResponse)) { // [IF Failed upload/no results] 
       Log.d(logtag, "failed photo upload/no response"); 
     } else { // [ELSEIF successful upload] 
      fbPhotoAddress = "https://www.facebook.com/photo.php?fbid=" +graphResponse;        
     } // [ENDIF successful posting or not] 
    } // [END onCompleted] 
    }; 

//Part 2: upload the photo 
    Request request = Request.newUploadPhotoRequest(session, imageSelected, uploadPhotoRequestCallback); 
    request.executeAsync(); 
+0

PeteH的答覆應該被標記爲答案!在postParams.putString(「picture」,fbPhotoAddress)中傳遞fbPhotoAddress;這將做到魔術。 – playmaker420

相關問題