How to Write a Weibo page (including inserting images, inserting emotices, inserting topics, inserting Location, and full code) (3)
If you insert the topic and the @ symbol:
Final class TopicHelper {private static final String TOPIC = # insert TOPIC name #; private WriteBlog activity; public void insertText () {final EditText editText = activity. etMblog; final int selectionStart = editText. getSelectionStart (); final int secletionEnd = editText. getSelectionEnd (); final String text = editText. getText (). toString (); if (selectionStart =-1 | secletionEnd =-1 | selectionStart> secletionEnd) {activity. etMblog. append (TopicHelper. TOPIC); final int totalLength = activity. etMblog. getText (). toString (). length (); activity. etMblog. setSelection (totalLength-TopicHelper. TOPIC. length () + 1, totalLength-1);} else {final StringBuilder sb = new StringBuilder (); sb. append (text. substring (0, selectionStart); sb. append (TOPIC); sb. append (text. substring (secletionEnd); editText. setText (sb. toString (); Selection. setSelection (editText. getText (), selectionStart + 1, selectionStart + TOPIC. length ()-1) ;}} TopicHelper (WriteBlog activity) {super (); this. activity = activity ;}}
Final class AtEditHelper {private static final String AT =@ enter the user name; private WriteBlog activity; public void insertText () {final EditText editText = activity. etMblog; final int selectionStart = editText. getSelectionStart (); // obtain the final int secletionEnd = editText where the cursor is located. getSelectionEnd (); final String text = editText. getText (). toString (); if (selectionStart =-1 | secletionEnd =-1 | selectionStart> secletionEnd) {activity. etMblog. append (AtEditHelper. AT); final int totalLength = activity. etMblog. getText (). toString (). length (); activity. etMblog. setSelection (totalLength-AtEditHelper. AT. length () + 1, totalLength-1);} else {final StringBuilder sb = new StringBuilder (); sb. append (text. substring (0, selectionStart); sb. append (AtEditHelper. AT); sb. append (text. substring (secletionEnd); editText. setText (sb. toString (); Selection. setSelection (editText. getText (), selectionStart + 1, selectionStart + AtEditHelper. AT. length ()-1) ;}} AtEditHelper (WriteBlog activity) {super (); this. activity = activity ;}}
When you exit Weibo, you need to save the image and content:
// DEFAULT_DRAFT_TEXT_PATH = getFilesDir (). getAbsolutePath () +/draft/text. dat; private boolean saveDraft (String text) {try {File f = new File (DEFAULT_DRAFT_TEXT_PATH); FileUtils. makesureFileExist (DEFAULT_DRAFT_TEXT_PATH); FileUtils. saveObject (text, new FileOutputStream (DEFAULT_DRAFT_TEXT_PATH);} catch (final Exception e) {} return false;} // This method ensures that the path has public static void makesureFileExist (String path) {String separator = File. separator; int index = path. lastIndexOf (separator); path = path. substring (0, index); File f = new File (path); f. mkdirs (); File f1 = new File (path); try {f1.createNewFile ();} catch (IOException e) {e. printStackTrace () ;}} public static void saveObject (String text, FileOutputStream fos) {try {ObjectOutputStream oos = new ObjectOutputStream (fos); oos. writeObject (text); oos. flush (); oos. close ();} catch (IOException ex) {ex. printStackTrace ();}}
Save image:
// Both path1 and path2 should be the pathpublic static void copy (String inputFilePath, String path2) {String separator = File. separator; int index = path2.lastIndexOf (separator); String path = path2.substring (0, index); File temp = new File (path); if (! Temp. exists () {temp. mkdirs ();} File tempFile = new File (path2); try {tempFile. createNewFile ();} catch (IOException e1) {e1.printStackTrace ();} FileInputStream inputStream = null; FileOutputStream outputStream = null; try {inputStream = new FileInputStream (inputFilePath ); outputStream = new FileOutputStream (path2); byte buffer [] = new byte [4*1024]; int len =-1; while (len = inputStream. read (buffer ))! =-1) {outputStream. write (buffer, 0, len);} outputStream. flush ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}
//defaultDraftPath = activity.getFilesDir().getAbsolutePath()+ /draft/bitmap_temp.jpg;// defaultTempPath = activity.getFilesDir().getAbsolutePath()+ /pic/ + System.currentTimeMillis() + .jpg;void saveDraft() {FileUtils.copy(defaultTempPath, defaultDraftPath);}
When you enter this page again, load the page content:
private void loadDraft() {String str = FileUtils.loadObject(DEFAULT_DRAFT_TEXT_PATH);etMblog.setText(str);etMblog.setSelection(str.length());if (mImageLoadingHelper.doesDraftExist()) {mImageLoadingHelper.loadDraft();}}public static String loadObject(String str) { Object obj = null; File f = new File(str);try {FileInputStream fis = new FileInputStream(f);ObjectInputStream ois = new ObjectInputStream(fis);obj = ois.readObject();} catch (Exception ex) {ex.printStackTrace();}return (String)obj;}void loadDraft() {FileUtils.copy(defaultDraftPath, defaultTempPath);activity.displayInsertBitmap();}