2012-09-10 102 views
0

我正嘗試在Facebook上上傳圖片。使用此代碼在Facebook上發佈圖片不起作用

 private void getFb() 
    { 

     final SharedPreferences mPrefs = getSharedPreferences(
       "fbPref", Activity.MODE_PRIVATE); 

     String access_token = mPrefs.getString("access_token", null); 
     long expires = mPrefs.getLong("access_expires", 0); 
     if(access_token != null) { 
      fb.setAccessToken(access_token); 
     } 
     if(expires != 0) { 
      fb.setAccessExpires(expires); 
     } 

     if(!fb.isSessionValid()) 
     { 
      fb.authorize(this, new String[] {"publish_stream"}, new DialogListener() { 
       public void onComplete(Bundle values) { 
        SharedPreferences.Editor editor = mPrefs.edit(); 
        editor.putString("access_token", fb.getAccessToken()); 
        editor.putLong("access_expires", fb.getAccessExpires()); 
        editor.commit(); 
       } 

       public void onFacebookError(FacebookError error) {} 

       public void onError(DialogError e) {} 

       public void onCancel() {} 
      }); 
     } 

    } 



    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     fb.authorizeCallback(requestCode, resultCode, data); 

     Bundle parameters = new Bundle(); 
    //  parameters.putString(Facebook.TOKEN, fb.getAccessToken()); 
     // The byte array is the data of a picture. 
     parameters.putString("message", "LolCats Test"); 
parameters.putString("picture", "http://cdn3.f-cdn.com/ppic/514992/logo/597178/profile_logo_597178.jpg"); 
parameters.putString("caption", "LLoll casts"); 

     try { 
      fb.request("me/photos", parameters, "POST"); 

     } catch (FileNotFoundException fileNotFoundException) { 
      // makeToast(fileNotFoundException.getMessage()); 
     } catch (MalformedURLException malformedURLException) { 
      // makeToast(malformedURLException.getMessage()); 
     } catch (IOException ioException) { 
     // makeToast(ioException.getMessage()); 
     } 

    } 

發生的與FB應用程序打開一個頁面,做一些進度條,然後返回給應用程序的唯一的事,沒有錯誤等什麼是錯的沒有獲得在Facebook上共享圖像?

回答

0

去到與此代碼工作....

private void getFb() 
     { 
      fb = new Facebook(Constants.facebookKey); 
      String[] arrayOfString = new String[1]; 
      arrayOfString[0] = "publish_stream"; 
      fb.authorize(this, arrayOfString, new FBDialogListener()); 
     } 

     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) 
     { 
      super.onActivityResult(requestCode, resultCode, data); 
      fb.authorizeCallback(requestCode, resultCode, data); 
     } 


     private class FBDialogListener implements Facebook.DialogListener 
     { 

      public void onComplete(Bundle values) 
      { 
       Bundle localBundle = new Bundle(); 
       String str1 = "Myapp"; 
       String str2 = "This image is by Myapp, app for Android."; 
       String str3 = "Download It"; 
       Object[] arrayOfObject = new Object[6]; 
        arrayOfObject[0] = str1; 
        arrayOfObject[1] = str2; 
        arrayOfObject[2] = bitmapUrls.get(currentPosition); 
        arrayOfObject[3] = bitmapUrls.get(currentPosition); 
        arrayOfObject[4] = str3; 
        arrayOfObject[5] = "http://play.google.com/store/apps/details?id=myapp"; 
        localBundle.putString("attachment", String.format("{\"name\":\"%s:\",\"href\":\"\",\"caption\":\"\",\"description\":\"%s\",\"media\":[{\"type\":\"image\",\"src\":\"%s\",\"href\":\"%s\"}],\"properties\":{\"%s\":{\"text\":\"Android market\",\"href\":\"%s\"}}}", arrayOfObject)); 
        fb.dialog(ImageActivity.this, "stream.publish", localBundle, new PublishDialogListener()); 

      } 

      public void onFacebookError(FacebookError e) { 
      } 

      public void onError(DialogError e) {     
      } 

      public void onCancel() { 

      } 
     } 

     private class PublishDialogListener implements Facebook.DialogListener 
     { 

      public void onComplete(Bundle values) { 
       Toast.makeText(ImageActivity.this, "Image has been shared" , Toast.LENGTH_LONG).show(); 
      } 

      public void onFacebookError(FacebookError e) { 
      } 

      public void onError(DialogError e) { 
      } 

      public void onCancel() { 
      } 

     } 
3

請試試這個。

parameters.putString("message", "LolCats Test"); 
parameters.putByteArray("source", "http://cdn3.fcdn.com/ppic/514992/logo/597178/profile_logo_597178.jpg".getBytes()); 
+0

做了什麼都沒有發生 –

0

此代碼的工作對我來說Bitmap bi=BitmapFactory.decodeResource(getResources(), rec_id); ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putByteArray("photo", data); params.putString("caption", status); AsyncFacebookRunner mAsyncRunner=new AsyncFacebookRunner(fb); mAsyncRunner.request("me/photos", parems,"POST",new mRequestListener(),null);
看到更多的細節例如在here

+0

查收,如果你是使用完全我的代碼與此添加或您有任何其他方式?請問您可以向我展示您的整個uploadImageOnFb功能嗎? –

+0

請檢查鏈接中的「Hackbook」示例...它非常有用,可以幫助您解決問題 –