The ability to publish a microblog is really simple, and we just need to invoke the method in the SDK. A little bit more trouble is to publish a microblog containing images, today mainly describes how to get the image path, and to publish the picture of the microblog.
Let's talk about my idea : When the user clicks the Add Picture button, I implement two methods, one is to call the camera, and the other is to use the system library to open the picture. The two methods are to return the picture path, through the picture path we can read the picture, and then upload to the server can be published with the image of the microblog.
To publish the Weibo interface:
The user clicks the Insert Picture button and calls the Showmenudialog () method:
private void Showmenudialog () {//Get picture path View Menuview = View.inflate (Updatestatus.this, R.layout.upload_dialog_menu, n
ULL); Final Alertdialog Menudialog = new Alertdialog.builder (updatestatus.this). Setview (Menuview). Settitle ("Insert Picture"). SetI
Con (r.drawable.ic_upload_photo). Create ();
Menudialog.show (); Menuview.findviewbyid (R.id.camera). Setonclicklistener (New Onclicklistener () {@Override public void OnClick (View
V) {Menudialog.dismiss (); Intent Intentcamera = new Intent ("Android.media.action.IMAGE_CAPTURE");//Use Camera Startactivityforresult (Intentcamera,
Activity.default_keys_dialer);
}
}); Menuview.findviewbyid (r.id.picture). Setonclicklistener (New Onclicklistener () {@Override public void OnClick (Vie
W v) {Menudialog.dismiss ();
Intent Intent = new Intent ();
/* Turn on the pictures screen type set to Image */Intent.settype ("image/*");
/* Use intent.action_get_content this ACTION */ Intent.setaction (intent.action_get_content);
/* Return to this screen after obtaining the photo */Startactivityforresult (intent, 2);
}
}); }
In order to get the picture path, we need to rewrite onactivityresult (int requestcode, int resultcode, Intent data) method:
@Override
protected void onactivityresult (int requestcode, int resultcode, Intent data) {
if (ResultCode = = Resul T_OK) {
if (null = = data) {
toastutil.showtoast (this, "Add Picture Failed!");
return;
}
Uri Thisuri = Data.getdata ();
FileName = Getrealpathfromuri (Thisuri); Get picture path by Uri
}
super.onactivityresult (Requestcode, ResultCode, data);
}
The Getrealpathfromuri () method is as follows:
/**
* Get the true path of the file stored locally by URI
* @param Act
* @param contenturi
* @return */
private String Getrealpathfromuri (Uri uri) {
//can post image
//Specify get column
String columns[] = new string[]{media.data, media._ ID, Media.title, media.display_name};
cursor cursor = this.managedquery (URI, columns,//Which columns to return
NULL,//WHERE clause; which rows to Retur N (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);
}
Get to the picture path, we can read the picture. Then we'll be able to post a micro-blog. How to publish a Weibo blog:
/** * Post Weibo * Weibo content cannot be empty and the number of words is less than */private void UpdateStatus () {String statustext = Statusedittext.gettext (). tostr
ING ();
if ("". Equals (StatusText) && statustext.length () > 140) {//Weibo content cannot be empty and the word count is less than 140. Alertdlgutil.alertdialog (updatestatus.this);//null prompt}else{if (statustext.length () >) {Toastutil.showtoas T (this, "no more than 140 words!");
Greater than 140 characters when} if (Checknetutil.checknet (updatestatus.this)) {//Check whether the network is available if ("". Equals (FileName)) {//does not contain a picture
thread = new Serverthread (Weibo, StatusText, false, handler);
Thread.Start ();
}else{//SYSTEM.OUT.PRINTLN ("filename-------;:" + filename); byte[] content = imagereaderutil.readfileimage (fileName)///SYSTEM.OUT.PRINTLN ("Content length:" + Content.len
GTH);
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); }
}
}
With these steps, we are ready to post a microblog.
If you have any questions, please leave a message and learn to communicate.
Description: Reprint please indicate the source.