Tweet di immagini Android

Sto cercando di tweetare l'image dalla cartella di tabs SD, ma ancora non posso farlo. Ho m utilizzando twitter4j-core-3.0.3 api e ho dato permessi android.permission.INTERNET , android.permission.ACCESS_NETWORK_STATE . Ecco il mio codice

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); mSharedPreferences = getApplicationContext().getSharedPreferences( "MyPref", 0); twitterButton = (Button) findViewById(R.id.twitPic); twitterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new ImageSender().execute(); } }); if (!isTwitterLoggedInAlready()) { Uri uri = getIntent().getData(); if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) { // oAuth verifier String verifier = uri .getQueryParameter(URL_TWITTER_OAUTH_VERIFIER); try { requestToken = twitter .getOAuthRequestToken(TWITTER_CALLBACK_URL); // Get the access token accessToken = twitter.getOAuthAccessToken( requestToken, verifier); // Shared Preferences Editor e = mSharedPreferences.edit(); // After getting access token, access token secret // store them in application preferences e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken()); e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret()); // Store login status - true e.putBoolean(PREF_KEY_TWITTER_LOGIN, true); e.commit(); // save changes Log.e("Twitter OAuth Token", "> " + accessToken.getToken()); } catch (Exception e) { // Check log for login errors Log.e("Twitter Login Error", "> " + e.getMessage()); } } } } private class ImageSender extends AsyncTask<URL, Integer, Long> { private String url; protected void onPreExecute() { pDialog = ProgressDialog.show(MainActivity.this, "", "Sending image...", true); pDialog.setCancelable(false); pDialog.show(); } protected Long doInBackground(URL... urls) { long result = 0; Log.d(TAG, "Start sending image..."); try { String ExternalStorageDirectoryPath = Environment .getExternalStorageDirectory() .getAbsolutePath(); String targetPath = ExternalStorageDirectoryPath + "/Friends/"+"image4.jpg"; File targetDirector = new File(targetPath); ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); // Access Token String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, ""); // Access Token Secret String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, ""); AccessToken accessToken = new AccessToken(access_token, access_token_secret); twitter = new TwitterFactory(builder.build()).getInstance(accessToken); StatusUpdate status = new StatusUpdate(""); status.setMedia(targetDirector); twitter.updateStatus(status); result = 1; Log.d(TAG, "Image uploaded, Twitpic url is " + url); } catch (TwitterException e) { Log.e(TAG, "Failed to send image "+e); e.printStackTrace(); } return result; } protected void onProgressUpdate(Integer... progress) { } protected void onPostExecute(Long result) { pDialog.cancel(); String text = (result == 1) ? "Image sent successfully.\n Twitpic url is: " + url : "Failed to send image"; Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show(); } } private boolean isTwitterLoggedInAlready() { // return twitter login status from Shared Preferences return mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false); } 

Ecco il mio log

  • Rilevamento a scorrimento per each row di listview
  • Android - Internet radio streaming
  • Come get l'ID di risorse Android da livelli-elenco / LayerDrawable creato dynamicmente (in Java)?
  • Accesso non definito chiave Google API API
  • tieni lo stato selezionato del button android
  • come get i giorni correnti delle settimane nel calendario
  •  04-02 06:38:02.275: E/Tag(1762): Failed to send image 400:The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting(https://dev.twitter.com/pages/rate-limiting). In API v1.1, a request without authentication is considered invalid and you will get this response. 04-02 06:38:02.275: E/Tag(1762): message - Bad Authentication data 04-02 06:38:02.275: E/Tag(1762): code - 215 04-02 06:38:02.275: E/Tag(1762): Relevant discussions can be found on the Internet at: 04-02 06:38:02.275: E/Tag(1762): http://www.google.co.jp/search?q=b2b52c28 or 04-02 06:38:02.275: E/Tag(1762): http://www.google.co.jp/search?q=11331d43 04-02 06:38:02.275: E/Tag(1762): TwitterException{exceptionCode=[b2b52c28-11331d43], statusCode=400, message=Bad Authentication data, code=215, retryAfter=-1, rateLimitStatus=null, version=3.0.3} 

    Modifica: quando provo a tweet un text in questa porzione

     StatusUpdate status = new StatusUpdate(""); status.setMedia(targetDirector); twitter.updateStatus(status); 

    a twitter.updateStatus("If you're reading this on Twitter, it worked!"); allora crea lo stesso errore.

    Ho da qualche giorno, ma non ho soluzione. Per favore qualcuno mi aiuta a risolvere il problema. Grazie

  • come cancellare il supporto di superficie quando il lettore multimediale è terminato?
  • L'apertura di un link del browser tramite la notifica non funziona
  • Come registrare audio sull'usura android
  • Scrittura di contenuti personalizzati per le foto sul telefono (parte 2)
  • Perché il valore timeout non è rispettato da HttpURLConnection di android?
  • Come uguagliare il field dell'adattatore personalizzato nel field del database
  • 2 Solutions collect form web for “Tweet di immagini Android”

    cambiare l'utilizzo di AsyncTask questo modo sta funzionando per me

      twitterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String status = null; new updateTwitterStatus().execute(status); } }); public class updateTwitterStatus extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); ProgressBar_show(); } protected String doInBackground(String... args) { try { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); // Access Token access_token = SharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, ""); // Access Token Secret access_token_secret = SharedPreferences.getString(PREF_KEY_OAUTH_SECRET, ""); String upload_image_url = postPicture( "/mnt/sdcard/yourimage.jpg", " "); Log.d("--------------upload_image_url=" + upload_image_url.toString() + "---------", " "); } catch (Exception e) { Log.d("Twitter Update Error", e.getMessage()); } return null; } public String postPicture(String fileName, String message) { try { Log.d("----start---postPicture()---", " "); File file = new File(fileName); MediaProvider mProvider = getMediaProvider(); String accessTokenToken = access_token; String accessTokenSecret = access_token_secret; Properties props = new Properties(); props.put(PropertyConfiguration.MEDIA_PROVIDER, mProvider); props.put(PropertyConfiguration.OAUTH_ACCESS_TOKEN, accessTokenToken); props.put(PropertyConfiguration.OAUTH_ACCESS_TOKEN_SECRET, accessTokenSecret); props.put(PropertyConfiguration.OAUTH_CONSUMER_KEY, TWITTER_CONSUMER_KEY); props.put(PropertyConfiguration.OAUTH_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET); Configuration conf = new PropertyConfiguration(props); ImageUploadFactory factory = new ImageUploadFactory(conf); ImageUpload upload = factory.getInstance(mProvider); String url; url = upload.upload(file, message); Log.d("----end---postPicture()---", " "); return url; } catch (Exception e) { e.printStackTrace(); } return null; } MediaProvider getMediaProvider() { Log.d("----start---getMediaProvider()---", " "); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); String provider = preferences.getString("pictureService", "twitter"); MediaProvider mProvider; if (provider.equals("yfrog")) mProvider = MediaProvider.YFROG; else if (provider.equals("twitpic")) mProvider = MediaProvider.TWITPIC; else if (provider.equals("twitter")) mProvider = MediaProvider.TWITTER; else throw new IllegalArgumentException("Picture provider " + provider + " unknown"); Log.d("----end---getMediaProvider()---", " "); return mProvider; } protected void onPostExecute(String file_url) { ProgressBar_hide(); // updating UI from Background Thread runOnUiThread(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), "Status tweeted successfully", Toast.LENGTH_SHORT).show(); } }); } } 

    Passare attraverso il seguente link, spiega come integrare twitter e tweet un'image dalla tua applicazione: http://www.londatiga.net/it/how-to-send-image-to-twitpic-from-android/ . Puoi scaricare la sua fonte completa da: https://github.com/lorensiuswlt/AndroidTwitpic

    L'Android è un fan Android di Google, tutto su telefoni Android, Android Wear, Android Dev e applicazioni Android Games e così via.