Code to Upload any image into Twitter from Android app after integration. | Techbirds

class updateTwitterImageStatus extends AsyncTask {

/** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage(“Updating to twitter…”); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show();

}

protected String doInBackground(String… args) {

File file = new File(Environment.getExternalStorageDirectory() + “/screenshot.jpg”); try { StatusUpdate status = new StatusUpdate(“upload image”); status.setMedia(file); twitter.updateStatus(status); } catch (TwitterException e) { Log.d(“TAG”, “Pic Upload error” + e.getErrorMessage()); // throw e; e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace();

}

return null;
}

/** * After completing background task Dismiss the progress dialog and show * the data in UI Always use runOnUiThread(new Runnable()) to update UI * from background thread, otherwise you will get error * **/ protected void onPostExecute(String file_url) { // dismiss the dialog after getting all products pDialog.dismiss(); // updating UI from Background Thread runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), “Status updated successfully”, Toast.LENGTH_SHORT) .show(); // Clearing EditText field // txtUpdate.setText(“”); } });

}

}

396 total views, 1 views today

Share this Onfacebook-1844397twitter-3259911linkedin-8829641google-2411882