Android text-to-text hybrid insertion and upload (I)-implements EditText text hybrid insertion and upload
An Android Conference Management System was developed some time ago. The project needs to involve the text and text mixing of EditText,
In the "meeting details", you must support text and image insertion. The following is an example of the input:
After the meeting is created, save the data to the server and view the created meeting,
1. clarify requirements
First, click the "meeting details" text box, normally enter the text, and then click the image icon in the lower left corner to enter the system album to select an image and insert it into the text box, you can also move the cursor in the middle of any text to insert the image. You can delete the text or image one by one after the image is created. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxzdHJvbmc + tv6hosq1z9bLvMK3PC9zdHJvbmc + PC9wPg0KPHByZSBjbGFzcz0 = "brush: java;"> To display images in an EditText file, you must first take a look at the use of SpannableString and ImageSpan. In the preceding figure, although the graphic effects are vivid, The get Text (). toString () Output of EditText is actually: "insert an image. Insert an image. ". That is to say, when I select to insert an image into EditText, although the image is displayed, it is actually the url of the image. When I save this record, the value sent to the server is: "insert an image. Insert an image. "The Code is as follows:
1. Click the image button to go to the system album.
/*** Select image on the graphic details page */public void getImage () {intent = new Intent (Intent. ACTION_GET_CONTENT); intent. addCategory (Intent. CATEGORY_OPENABLE); intent. setType ("image/*"); startActivityForResult (intent, 0 );}
2. Get the image and call the interface to upload the image to the server. After the upload is successful, get the url of the image returned by the server.
@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = RESULT_ OK & requestCode = 0) {ContentResolver resolver = getContentResolver (); // retrieve the image uri Uri originalUri = data. getData (); bitmap = null; try {Bitmap originalBitmap = BitmapFactory. decodeStream (resolver. openInputStream (originalUri); bitmap = ImageUtils. resizeImage (originalBitmap, 600); // convert the bitmap of the original image to a file // upload the file and obtain the url new Thread (new Runnable () {@ Override public void run () {insertPic (bitmap, 0 );}}). start ();} catch (FileNotFoundException e) {e. printStackTrace ();}}}
3. Execute the insertPic () method to obtain the url and perform some processing to display it in edittext.
/*** Insert image */private void insertPic (Bitmap bm, final int index) {AjaxParams params = new AjaxParams (); try {params. put ("image", LeoUtils. saveBitmap (bm);} catch (FileNotFoundException e) {e. printStackTrace ();} FinalHttp fh = new FinalHttp (); System. out. println ("params =" + params. toString (); fh. post (HttpUrlConstant. UPLOAD_PIC, params, new AjaxCallBack() {@ Override public void onFailure (Throwable t, int errorNo, String strMsg) {super. onFailure (t, errorNo, strMsg); ToastUtil. show (getApplicationContext (), "Image Upload Failed, please check the network") ;}@ Override public void onSuccess (Object t) {super. onSuccess (t); System. out. println (t. toString (); try {JSONObject jsonObject = new JSONObject (t. toString (); String url = jsonObject. getString ("recordName"); switch (index) {case 0: // create an ImageSpan object ImageSpan imageSpan = new ImageSpan (CreateMeetingActivity) based on the Bitmap object. this, bitmap); // create a SpannableString object to insert the image String tempUrl = ""; SpannableString spannableString = new SpannableString (tempUrl) encapsulated by the ImageSpan object ); // replace your specified string with the ImageSpan object. setSpan (imageSpan, 0, tempUrl. length (), Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); // append the selected image to EditText at the cursor position int index = et_detail.getSelectionStart (); // obtain the cursor position Editable edit_text = et_detail.getEditableText (); if (index <0 | index> = edit_text.length () {edit_text.append (spannableString);} else {edit_text.insert (index, spannableString);} System. out. println ("inserted image:" + spannableString. toString (); break; case 1: // code irrelevant to this case;} catch (JSONException e) {e. printStackTrace ();}}});}
The above annotations are detailed. Even if you do not know SpannerString and ImageSpan before, I believe you can understand their usage. Now, the text-and-text hybrid insertion requirement for android edittext has been completed.
Iii. Additional instructions
Q1: Why do I need to upload an image to the server to obtain the url?
A1: PM requires that the interface be called to upload the image to the server every time an image is inserted. This interface will return the url of the image, although this requirement is not a good solution. If you do not need to save the text and text inserted content, you do not need to perform this step. You can replace the imageSpan with any character, because for spannebleString, the inserted image is actually only a few characters, which can output edittext. gettext (). tostring.
// Use the ImageSpan object to replace spannableString. setSpan (imageSpan, 0, "Image 1", Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
Q2: Why do I need to wrap the image url into a string with the img tag?
A2: Another requirement of the project is to edit the meeting, that is, the content inserted by the image and text mixture can be edited again later. When you call the edit meeting interface, the system returns the meeting details. The data is: "insert an image. Insert an image. "In order to display the text on the meeting details page back to EditText, I will use regular expression matching for these strings." If this format is matched, it indicates that it is an image, then, the image is displayed after some processing.