Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 3.03 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. android facebook app not posting image
  2. public class DummyActivity extends Activity {
  3.  
  4.     Facebook facebook = new Facebook("xxxxxxxxxx");
  5.  
  6.     @Override
  7.     public void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.main);
  10.  
  11.         facebook.authorize(this, new DialogListener() {
  12.             public void onComplete(Bundle values) {
  13.                  byte[] data = null;
  14.  
  15.                  Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
  16.                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  17.                  bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  18.                  data = baos.toByteArray();
  19.  
  20.                  Bundle params = new Bundle();
  21.                  params.putString(Facebook.TOKEN, facebook.getAccessToken());
  22.                  params.putString("method", "photos.upload");
  23.                  params.putByteArray("picture", data);
  24.  
  25.                  AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
  26.                  mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
  27.             }
  28.  
  29.             public void onFacebookError(FacebookError error) {}
  30.  
  31.             public void onError(DialogError e) {}
  32.  
  33.             public void onCancel() {}        
  34.         });
  35.     }
  36.  
  37.  
  38.  
  39.  
  40.     @Override
  41.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  42.         super.onActivityResult(requestCode, resultCode, data);
  43.  
  44.         facebook.authorizeCallback(requestCode, resultCode, data);
  45.     }
  46.        
  47. public class SampleUploadListener implements RequestListener {
  48.  
  49.     public void onComplete(String response, Object state) {
  50.         // TODO Auto-generated method stub
  51.           try {
  52.                 // process the response here: (executed in background thread)
  53.                 Log.d("Facebook-Example", "Response: " + response.toString());
  54.                 JSONObject json = Util.parseJson(response);
  55.                 final String src = json.getString("src");
  56.  
  57.                 // then post the processed result back to the UI thread
  58.                 // if we do not do this, an runtime exception will be generated
  59.                 // e.g. "CalledFromWrongThreadException: Only the original
  60.                 // thread that created a view hierarchy can touch its views."
  61.  
  62.             } catch (JSONException e) {
  63.                 Log.w("Facebook-Example", "JSON Error in response");
  64.             } catch (FacebookError e) {
  65.                 Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
  66.             }
  67.  
  68.     }
  69.  
  70.     public void onIOException(IOException e, Object state) {
  71.         // TODO Auto-generated method stub
  72.  
  73.     }
  74.  
  75.     public void onFileNotFoundException(FileNotFoundException e, Object state) {
  76.         // TODO Auto-generated method stub
  77.  
  78.     }
  79.  
  80.     public void onMalformedURLException(MalformedURLException e, Object state) {
  81.         // TODO Auto-generated method stub
  82.  
  83.     }
  84.  
  85.     public void onFacebookError(FacebookError e, Object state) {
  86.         // TODO Auto-generated method stub
  87.  
  88.     }