2012-11-07 81 views
2

我遇到了一些问题,每当我尝试发布图片到Facebook,类似下面的屏幕截图总是显示一个错误。Facebook SDK图片发布错误

它适用于仿真器,但不适用于设备。

我用旧的,也尝试了新的Facebook的SDK(3.0) 为Android没有成功。

Facebook error upon attempting to post image

这里是我的Facebook分享应用

public class FacebookShare extends Activity 
{ 
    private String      APP_ID, APP_SECRET, Name, Link, Description, Picture; 
    private int       fbTYPE; 
    private Facebook     facebook; 
    private AsyncFacebookRunner mAsyncRunner; 
    private Activity     ctx; 
    private Bitmap      bitmap; 

    SharedPreferences     mPrefs; 

    public FacebookShare(Activity ctx) 
    { 
     APP_ID = "...obfuscated..."; 

     facebook = new Facebook(APP_ID); 
     mAsyncRunner = new AsyncFacebookRunner(facebook); 
     this.ctx = ctx; 
    } 

    public void shareFB(int TypeOfSharing) 
    { 
     APP_ID = "...obfuscated..."; 
     facebook = new Facebook(APP_ID); 
     mAsyncRunner = new AsyncFacebookRunner(facebook); 

     this.fbTYPE = TypeOfSharing; 
     loginToFacebook(); 
    } 

    public void loginToFacebook() 
    { 
     Log.v("debugging", "Entered Login to facebook"); 
     String access_token = mPrefs.getString("access_token", ""); 
     long expires = mPrefs.getLong("access_expires", 0); 

     if (!access_token.equals("")) 
     { 
      facebook.setAccessToken(access_token); 

      Log.v("Access Token", facebook.getAccessToken()); 
     } 

     if (expires != 0) 
     { 
      facebook.setAccessExpires(expires); 
     } 

     if (!facebook.isSessionValid()) 
     { 
      Log.v("debugging", "Session is Invalid"); 

      facebook.authorize(ctx, new String[]{ 
      "email","publish_stream" 
      }, new DialogListener() 
      { 

       public void onCancel() 
       { 
        // Function to handle cancel event 
       } 

       public void onComplete(Bundle values) 
       { 
        // Function to handle complete event 
        // Edit Preferences and update facebook acess_token 

        SharedPreferences.Editor editor = mPrefs.edit(); 
        editor.putString("access_token", facebook.getAccessToken()); 
        editor.putLong("access_expires", facebook.getAccessExpires()); 
        editor.commit(); 

        if (fbTYPE == 1) 
        { 
         postToWall(); 
        } 
        else if (fbTYPE == 0) 
        { 
         postToWall(getBitmap()); 
        } 
       } 

       public void onError(DialogError error) 
       { 
        Log.v("debugging", error.getMessage()); 
       } 

       public void onFacebookError(FacebookError fberror) 
       { 
        Log.v("debugging", fberror.getMessage()); 
       } 

      }); 
      Log.v("debugging", "Passed from authorization"); 
     } 
     else 
     { 
      if (fbTYPE == 1) 
      { 
       Log.v("debugging", "Entered Post to facebook"); 
       postToWall(); 
      } 
      else if (fbTYPE == 0) 
      { 
       Log.v("debugging", "Entered Post image to facebook"); 
       postToWall(getBitmap()); 

      } 
     } 
    } 

    public void clearCredentials() 
    { 

     try 
     { 

      facebook.logout(ctx); 

     } 
     catch (MalformedURLException e) 
     { 

      e.printStackTrace(); 

     } 
     catch (IOException e) 
     { 

      e.printStackTrace(); 

     } 

    } 

    public void postToWall() 
    { 
     // post on user's wall. 
     Bundle params = new Bundle(); 
     params.putString("description", getDescription()); 
     params.putString("picture", getPicture()); 
     params.putString("name", getName()); 
     params.putString("link", getLink()); 

     facebook.dialog(ctx, "feed", params, new DialogListener() 
     { 

      public void onFacebookError(FacebookError e) 
      { 
       e.printStackTrace(); 
       Log.e("FBDEBUG", e.toString()); 
      } 

      public void onError(DialogError e) 
      { 
       e.printStackTrace(); 
       Log.e("FBDEBUG", e.toString()); 
      } 

      public void onComplete(Bundle values) 
      { 
       Toast.makeText(ctx, "Thanks for sharing JOLENPOP", Toast.LENGTH_SHORT).show(); 
      } 

      public void onCancel() 
      { 
       // Login_Activity.asyncFBLogin fblogin = null; 
       // fblogin.execute(); 
      } 
     }); 

    } 

    public void postToWall(Bitmap bmImage) 
    { 
     Log.v("debugging", "entered postToWall(bitmap)"); 
     byte[] data = null; 
     Bitmap bm = Bitmap.createBitmap(bmImage); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bm.compress(CompressFormat.JPEG, 100, baos); 
     data = baos.toByteArray(); 

     Bundle params = new Bundle(); 
     params.putString("method", "post"); 
     params.putString("message", getDescription()); 
     params.putByteArray("image", data); 

     try 
     { 
      String response = facebook.request("me"); 
      response = facebook.request("me/photos", params, "POST"); 
      if (response == null || response.equals("") || response.equals("false")) 
      { 
       Log.v("response String", response); 
       return; 
      } 
      else if (response.toLowerCase().contains("error")) 
      { 
       Log.v("response String", response); 
       return; 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      Log.e("FBDEBUG", e.toString()); 
      return; 
     } 

     Toast.makeText(ctx, "Your photo has been successfuly published!", Toast.LENGTH_LONG).show(); 
    } 

    public void getProfileInformation() 
    { 
     mAsyncRunner.request("me", new RequestListener() 
     { 
      public void onComplete(String response, Object state) 
      { 
       Log.d("Profile", response); 
       String json = response; 
       try 
       { 
        JSONObject profile = new JSONObject(json); 
        // getting name of the user 
        String name = profile.getString("name"); 
        // getting email of the user 
        String email = profile.getString("email"); 

        runOnUiThread(new Runnable() 
        { 

         public void run() 
         { 
          // Toast.makeText(getApplicationContext(), "Name: " + name 
          // + "\nEmail: " + email, Toast.LENGTH_LONG).show(); 
         } 

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

      public void onIOException(IOException e, Object state) 
      { 

      } 

      public void onFileNotFoundException(FileNotFoundException e, Object state) 
      { 

      } 

      public void onMalformedURLException(MalformedURLException e, Object state) 
      { 

      } 

      public void onFacebookError(FacebookError e, Object state) 
      { 

      } 
     }); 
    } 

    /** 
    * setters 
    * */ 
    public void setFacebook(Facebook facebook) 
    { 
     this.facebook = facebook; 
    } 

    public void setAsyncRunner(AsyncFacebookRunner mAsyncRunner) 
    { 
     this.mAsyncRunner = mAsyncRunner; 
    } 

    public void setPrefs(SharedPreferences mPrefs) 
    { 
     this.mPrefs = mPrefs; 
    } 

    public void setName(String val) 
    { 
     this.Name = val; 
    } 

    public void setLink(String val) 
    { 
     this.Link = val; 
    } 

    public void setBitmap(Bitmap val) 
    { 
     this.bitmap = val; 
    } 

    public void setDescription(String val) 
    { 
     this.Description = val; 
    } 

    public void setPicture(String val) 
    { 
     this.Picture = val; 
    } 

    /** 
    * getters 
    * */ 

    public String getAppID() 
    { 
     return this.APP_ID; 
    } 

    public String getName() 
    { 
     return this.Name; 
    } 

    public String getLink() 
    { 
     return this.Link; 
    } 

    public String getDescription() 
    { 
     return this.Description; 
    } 

    public String getPicture() 
    { 
     return this.Picture; 
    } 

    public Bitmap getBitmap() 
    { 
     return this.bitmap; 
    } 

} 

的类代码,这里是我如何使用它:

fbShare = new FacebookShare(this); 
mPrefs = PreferenceManager.getDefaultSharedPreferences(this); 

Log.v("debugging", "entered post to image"); 
Bitmap screenshot = this.glSurfaceView.mRenderer.screenCapture; 
fbShare.setName("JOLENPOP"); 
fbShare.setDescription("I got a score of " + this.glSurfaceView.mRenderer.Score + " in JOLENPOP! Try to beat me!"); 
fbShare.setBitmap(screenshot); 
fbShare.setPrefs(mPrefs); 
fbShare.shareFB(0); 

我试过的内置应用程序,也facebook.FORCE_DIALOG_AUTH在他们的结果没有任何区别..在此先感谢,更多的权力:)

+0

你可能想从你的代码中删除你的应用密码 –

+0

哦..谢谢编辑 – Keiichi

回答

2

我怀疑这与将参数设置为byte []而不是Bitmap有关。

最直接的路径可能是尝试使用Request.newUploadPhotoRequest()而不修改生成的请求。一旦你得到了这个工作,你可以添加额外的参数来定制帖子,或者查看该方法的实现,看看它是如何构建请求,如果你想做一个更深的变化。

+0

嗯...我检查了我们的其他应用程序,有facebook上传,他们也失败了,他们以前工作正常,我怀疑它有与Facebook平台更新有关。 – Keiichi