The function of posting Weibo is actually very simple. We only need to call the method in the SDK. A little troublesome is posting Weibo posts containing images. Today we will mainly introduce how to obtain image paths and publish Weibo posts with images.
Let's talk about it first.My ideas: When you click the Add image button, you can call the camera or use the system image library to open the image. Both methods return the image path. Through the image path, we can read the image and upload it to the server to publish a microblog with the image.
Weibo posting interface:
Click the insert image button and call the showmenudialog () method:
Private void showMenuDialog () {// obtain the image path View menuView = View. inflate (UpdateStatus. this, R. layout. upload_dialog_menu, null); final AlertDialog menuDialog = new AlertDialog. builder (UpdateStatus. this ). setView (menuView ). setTitle ("insert image "). setIcon (R. drawable. ic_upload_photo ). create (); menuDialog. show (); menuView. findViewById (R. id. camera ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {menuDialog. dismiss (); Intent intentCamera = new Intent ("android. media. action. IMAGE_CAPTURE "); // use the camera startActivityForResult (intentCamera, Activity. DEFAULT_KEYS_DIALER) ;}}); menuView. findViewById (R. id. picture ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {menuDialog. dismiss (); Intent intent = new Intent ();/* set Pictures image Type to image */intent. setType ("image/*");/* use Intent. ACTION_GET_CONTENT this Action */intent. setAction (Intent. ACTION_GET_CONTENT);/* returns the current screen after obtaining the photo */startActivityForResult (intent, 2 );}});}
To obtain the image path, we need to override the onactivityresult (INT requestcode, int resultcode, intent data) method:
@ Overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {If (resultcode = result_ OK) {If (null = data) {toastutil. showtoast (this, "failed to add image! "); Return;} uri thisuri = data. getdata (); filename = getrealpathfromuri (thisuri); // obtain the image path through URI} super. onactivityresult (requestcode, resultcode, data );}
The getrealpathfromuri () method is as follows:
/*** Obtain the real path of the file stored locally through URI * @ Param act * @ Param contenturi * @ return */private string getrealpathfromuri (URI) {// can post image // specify the string columns [] = new string [] {media. data, media. _ id, media. title, media. display_name}; cursor = This. managedquery (Uri, columns, // which columns to return NULL, // where clause; Which Rows to return (all rows) null, // where clause selection arguments (none) null); // order-by clause (Ascending by name) int column_index = cursor. getcolumnindexorthrow (mediastore. images. media. data); cursor. movetofirst (); Return cursor. getstring (column_index );}
After obtaining the image path, we can read the image. Then we can post our Weibo posts. Weibo posting method:
/***** Weibo content cannot be blank and the number of words is less than 140 */private void updateStatus () {String statusText = statusEditText. getText (). toString (); if ("". equals (statusText) & statusText. length ()> 140) {// Weibo content cannot be blank and the number of words is less than 140. alertDlgUtil. alertDialog (UpdateStatus. this); // prompt when it is null} else {if (statusText. length () & gt; 140) {ToastUtil. showToast (this, "the number of words cannot exceed 140! "); // When it exceeds 140 characters} if (CheckNetUtil. checkNet (UpdateStatus. this) {// check whether the Network is available if ("". equals (fileName) {// when the image is not included, thread = new ServerThread (weibo, statusText, false, handler); thread. start ();} else {// System. out. println ("fileName ------->:" + fileName); byte [] content = ImageReaderUtil. readFileImage (fileName); // when the image is included // System. out. println ("content length:" + content. length); ImageItem pic = new ImageItem ("pic", content); String s = java.net. URLEncoder. encode (statusText, "UTF-8"); thread = new ServerThread (weibo, s, pic, true, handler); thread. start () ;}} else {AlertDlgUtil. alertDialogNetErr (UpdateStatus. this );}}}
Through the above steps, we can publish Weibo.
If you have any questions, please leave a message so that you can learn and communicate with each other!
Note: Please indicate the source for reprinting!