简体   繁体   中英

Image not posted on facebook by android app

Hi I am using a tutorial to post image on facebook but its not posting. Please guide what i am doing wrong. I have facebook.apk installed on mobile. I have done extactly what is described on Facebook developer guiide.

Here is the error.... 在此处输入图片说明

public class DummyActivity extends Activity {
Facebook facebook = new Facebook("xxxxxxxxxxxx");

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    facebook.authorize(this, new DialogListener() {
        public void onComplete(Bundle values) {
             byte[] data = null;

             Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
             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 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);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

Here is another class,

  public class SampleUploadListener implements RequestListener {

public void onComplete(String response, Object state) {
    // TODO Auto-generated method stub
      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());
        }

}

public void onIOException(IOException e, Object state) {
    // TODO Auto-generated method stub

}

public void onFileNotFoundException(FileNotFoundException e, Object state) {
    // TODO Auto-generated method stub

}

public void onMalformedURLException(MalformedURLException e, Object state) {
    // TODO Auto-generated method stub

}

public void onFacebookError(FacebookError e, Object state) {
    // TODO Auto-generated method stub

}

You are using wrong parametes!

Check the documentation of photos.upload

Change your code like this,

    Bundle params = new Bundle();

                try {
                    Bitmap bMap = BitmapFactory.decodeFile(config.downloaded_image_path);
                    Log.i("Path",config.downloaded_image_path);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    bMap.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object   
                    byte[] b = baos.toByteArray(); 
                    params.putByteArray("photo", b);
                } catch  (Exception e) {
                    e.printStackTrace();
                }
                params.putString("caption", config.facebook_comment);
                Utility.mAsyncRunner.request("me/photos", params, "POST", new PhotoUploadListener(), null);

Try changing the keyvalue for param as "photo" instead of "picture" and in the request method you are providing null for GRaphApi which is certainly wrong.

Try this one FB

it may helps you.

好的,我设法登录了,并发布了正确的答案...

02-13 14:48:55.949: D/Facebook-Example(2814): Response: {"pid":"100002384000781_493454","aid":"100002384000781_53368","owner":100002384000781,"src":"http:\/\/photos-h.ak.fbcdn.net\/hphotos-ak-ash4\/400189_233648050057993_100002384000781_493454_803419378_s.jpg","src_big":"http:\/\/a8.sphotos.ak.fbcdn.net\/hphotos-ak-ash4\/400189_233648050057993_100002384000781_493454_803419378_n.jpg","src_small":"http:\/\/photos-h.ak.fbcdn.net\/hphotos-ak-ash4\/400189_233648050057993_100002384000781_493454_803419378_t.jpg","link":"http:\/\/www.facebook.com\/photo.php?fbid=233648050057993&set=a.233615786727886.53368.100002384000781&type=1","caption":"","created":1329124670,"object_id":233648050057993}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM