Laden Sie ein Foto auf Facebook mit Facebook Android SDK in Android

Ich bin neu in Android. Ich bin auf der Suche nach laden Foto nach Facebook durch Autorisierung, erhalten access_token. Wie macht man das? Bitte geben Sie mir einen Beispielcode in Java.

Habe hier einfach den einfachen Weg zum Hochladen eines Fotos gepostet:

Android Facebook veröffentlichen Foto

Code:

byte[] data = null; Bitmap bi = BitmapFactory.decodeFile(photoToPost); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); data = baos.toByteArray(); Bundle params = new Bundle(); params.putString("method", "photos.upload"); params.putByteArray("picture", data); AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null); 
 facebook.authorize(FbdemoActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() { @Override public void onComplete(Bundle values) { } @Override public void onFacebookError(FacebookError error) { } @Override public void onError(DialogError e) { } @Override public void onCancel() { } }); public void postImageonWall() { byte[] data = null; Bitmap bi = BitmapFactory.decodeFile("/sdcard/viewitems.png"); 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 class SampleUploadListener extends BaseRequestListener { public void onComplete(final String response, final Object state) { 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 // eg "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()); } } @Override public void onFacebookError(FacebookError e, Object state) { // TODO Auto-generated method stub } } 

Hier ist der ganze Code, es zu versuchen, es wird definitiv für Sie arbeiten und es ist Arbeit für mich auch

Der einfachste Weg für Sie ist die Verwendung des vorhandenen SDK, etwa so:
http://github.com/facebook/facebook-android-sdk/
http://code.google.com/p/fbconnect-android/
http://wiki.developers.facebook.com/index.php/User:Android

Der flexiblere Weg ist, die API selbst zu implementieren, hier sind die Dokumente, die nützlich sein werden:
http://developers.facebook.com/docs/

Wenn Sie ein Foto mit einer Beschreibung veröffentlichen möchten, können Sie dies tun:

 public void publishPhoto(byte[] imgData, string message) { AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner (facebook); Bundle parameters = new Bundle (); parameters.putString (Facebook.TOKEN, facebook.getAccessToken()); parameters.putString ("message", message); parameters.putByteArray ("source", imgData); mAsyncRunner.Request ("me/photos", parameters, "POST", new RequestListener (), null); } 

Erfolgreich habe ich die Fotos auf Facebook Wand hochgeladen Ich habe den folgenden Code verwendet.

In Ihrer Hauptaktivität machen Sie folgendes;

 btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub authenticatedFacebook.authorize(Facebooktest2Activity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() { @Override public void onComplete(Bundle values) { postImage(); Toast.makeText(getApplicationContext(), "Image Posted on Facebook.", Toast.LENGTH_SHORT).show(); } @Override public void onFacebookError(FacebookError error) { } @Override public void onError(DialogError e) { } @Override public void onCancel() { } }); } }); } public void postImage(){ byte[] data = null; Bitmap bi = BitmapFactory.decodeFile("/sdcard/img.jpg"); //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); data = baos.toByteArray(); Bundle params = new Bundle(); params.putString(Facebook.TOKEN, authenticatedFacebook.getAccessToken()); params.putString("method", "photos.upload"); params.putByteArray("picture", data); AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(authenticatedFacebook); mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null); } public class SampleUploadListener extends BaseKeyListener implements RequestListener { public void onComplete(final String response, final Object state) { 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 // eg "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 onFacebookError(FacebookError e, Object state) { // TODO Auto-generated method stub } public Bitmap getInputType(Bitmap img) { // TODO Auto-generated method stub return img; } @Override public int getInputType() { // TODO Auto-generated method stub return 0; } @Override public void onIOException(IOException e, Object state) { // TODO Auto-generated method stub } @Override public void onFileNotFoundException(FileNotFoundException e, Object state) { // TODO Auto-generated method stub } @Override public void onMalformedURLException(MalformedURLException e, Object state) { // TODO Auto-generated method stub } } 

Sicherlich wird dieser Code Ihnen helfen.