66import android .graphics .Bitmap ;
77import android .net .Uri ;
88import android .os .Bundle ;
9- import android .support .design .widget .FloatingActionButton ;
10- import android .support .design .widget .Snackbar ;
119import android .support .v7 .widget .Toolbar ;
1210import android .text .TextUtils ;
1311import android .view .Menu ;
3331import com .zhihu .matisse .MimeType ;
3432import com .zhihu .matisse .internal .entity .CaptureStrategy ;
3533
36- import org .reactivestreams .Subscriber ;
37-
3834import java .util .Date ;
3935import java .util .List ;
4036
@@ -116,45 +112,49 @@ public void onClick(View v) {
116112
117113 openSoftKeyInput ();//打开软键盘显示
118114
119- Intent intent = getIntent ();
120- flag = intent .getIntExtra ("flag" , 0 );//0新建,1编辑
121- if (flag == 1 ){//编辑
122- setTitle ("编辑笔记" );
123- Bundle bundle = intent .getBundleExtra ("data" );
124- note = (Note ) bundle .getSerializable ("note" );
125-
126- if (note != null ) {
127- myTitle = note .getTitle ();
128- myContent = note .getContent ();
129- myNoteTime = note .getCreateTime ();
130- Group group = groupDao .queryGroupById (note .getGroupId ());
131- if (group != null ){
132- myGroupName = group .getName ();
133- tv_new_group .setText (myGroupName );
134- }
135-
136- loadingDialog = new ProgressDialog (this );
137- loadingDialog .setMessage ("数据加载中..." );
138- loadingDialog .setCanceledOnTouchOutside (false );
139- loadingDialog .show ();
140-
141- tv_new_time .setText (note .getCreateTime ());
142- et_new_title .setText (note .getTitle ());
143- et_new_content .post (new Runnable () {
144- @ Override
145- public void run () {
146- dealWithContent ();
115+ try {
116+ Intent intent = getIntent ();
117+ flag = intent .getIntExtra ("flag" , 0 );//0新建,1编辑
118+ if (flag == 1 ){//编辑
119+ setTitle ("编辑笔记" );
120+ Bundle bundle = intent .getBundleExtra ("data" );
121+ note = (Note ) bundle .getSerializable ("note" );
122+
123+ if (note != null ) {
124+ myTitle = note .getTitle ();
125+ myContent = note .getContent ();
126+ myNoteTime = note .getCreateTime ();
127+ Group group = groupDao .queryGroupById (note .getGroupId ());
128+ if (group != null ){
129+ myGroupName = group .getName ();
130+ tv_new_group .setText (myGroupName );
147131 }
148- });
149- }
150- } else {
151- setTitle ("新建笔记" );
152- if (myGroupName == null || "全部笔记" .equals (myGroupName )) {
153- myGroupName = "默认笔记" ;
132+
133+ loadingDialog = new ProgressDialog (this );
134+ loadingDialog .setMessage ("数据加载中..." );
135+ loadingDialog .setCanceledOnTouchOutside (false );
136+ loadingDialog .show ();
137+
138+ tv_new_time .setText (note .getCreateTime ());
139+ et_new_title .setText (note .getTitle ());
140+ et_new_content .post (new Runnable () {
141+ @ Override
142+ public void run () {
143+ dealWithContent ();
144+ }
145+ });
146+ }
147+ } else {
148+ setTitle ("新建笔记" );
149+ if (myGroupName == null || "全部笔记" .equals (myGroupName )) {
150+ myGroupName = "默认笔记" ;
151+ }
152+ tv_new_group .setText (myGroupName );
153+ myNoteTime = CommonUtil .date2string (new Date ());
154+ tv_new_time .setText (myNoteTime );
154155 }
155- tv_new_group .setText (myGroupName );
156- myNoteTime = CommonUtil .date2string (new Date ());
157- tv_new_time .setText (myNoteTime );
156+ } catch (Exception e ) {
157+ e .printStackTrace ();
158158 }
159159
160160 }
@@ -181,13 +181,17 @@ public void onRtImageDelete(String imagePath) {
181181 et_new_content .setOnRtImageClickListener (new RichTextEditor .OnRtImageClickListener () {
182182 @ Override
183183 public void onRtImageClick (String imagePath ) {
184- myContent = getEditData ();
185- if (!TextUtils .isEmpty (myContent )){
186- List <String > imageList = StringUtils .getTextFromHtml (myContent , true );
187- if (!TextUtils .isEmpty (imagePath )) {
188- int currentPosition = imageList .indexOf (imagePath );
189- showToast ("点击图片:" + currentPosition + ":" + imagePath );
184+ try {
185+ myContent = getEditData ();
186+ if (!TextUtils .isEmpty (myContent )){
187+ List <String > imageList = StringUtils .getTextFromHtml (myContent , true );
188+ if (!TextUtils .isEmpty (imagePath )) {
189+ int currentPosition = imageList .indexOf (imagePath );
190+ showToast ("点击图片:" + currentPosition + ":" + imagePath );
191+ }
190192 }
193+ } catch (Exception e ) {
194+ e .printStackTrace ();
191195 }
192196 }
193197 });
@@ -265,16 +269,20 @@ public void onSubscribe(Disposable d) {
265269
266270 @ Override
267271 public void onNext (String text ) {
268- if (et_new_content != null ) {
269- if (text .contains ("<img" ) && text .contains ("src=" )) {
270- //imagePath可能是本地路径,也可能是网络地址
271- String imagePath = StringUtils .getImgSrc (text );
272- //插入空的EditText,以便在图片前后插入文字
273- et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), "" );
274- et_new_content .addImageViewAtIndex (et_new_content .getLastIndex (), imagePath );
275- } else {
276- et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), text );
272+ try {
273+ if (et_new_content != null ) {
274+ if (text .contains ("<img" ) && text .contains ("src=" )) {
275+ //imagePath可能是本地路径,也可能是网络地址
276+ String imagePath = StringUtils .getImgSrc (text );
277+ //插入空的EditText,以便在图片前后插入文字
278+ et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), "" );
279+ et_new_content .addImageViewAtIndex (et_new_content .getLastIndex (), imagePath );
280+ } else {
281+ et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), text );
282+ }
277283 }
284+ } catch (Exception e ) {
285+ e .printStackTrace ();
278286 }
279287 }
280288 });
@@ -301,14 +309,18 @@ protected void showEditData(ObservableEmitter<String> emitter, String html) {
301309 * 负责处理编辑数据提交等事宜,请自行实现
302310 */
303311 private String getEditData () {
304- List <RichTextEditor .EditData > editList = et_new_content .buildEditData ();
305312 StringBuilder content = new StringBuilder ();
306- for (RichTextEditor .EditData itemData : editList ) {
307- if (itemData .inputStr != null ) {
308- content .append (itemData .inputStr );
309- } else if (itemData .imagePath != null ) {
310- content .append ("<img src=\" " ).append (itemData .imagePath ).append ("\" />" );
313+ try {
314+ List <RichTextEditor .EditData > editList = et_new_content .buildEditData ();
315+ for (RichTextEditor .EditData itemData : editList ) {
316+ if (itemData .inputStr != null ) {
317+ content .append (itemData .inputStr );
318+ } else if (itemData .imagePath != null ) {
319+ content .append ("<img src=\" " ).append (itemData .imagePath ).append ("\" />" );
320+ }
311321 }
322+ } catch (Exception e ) {
323+ e .printStackTrace ();
312324 }
313325 return content .toString ();
314326 }
@@ -322,50 +334,54 @@ private void saveNoteData(boolean isBackground) throws Exception {
322334 String groupName = tv_new_group .getText ().toString ();
323335 String noteTime = tv_new_time .getText ().toString ();
324336
325- Group group = groupDao .queryGroupByName (myGroupName );
326- if (group != null ) {
327- if (noteTitle .length () == 0 ){//如果标题为空,则截取内容为标题
328- if (noteContent .length () > cutTitleLength ){
329- noteTitle = noteContent .substring (0 ,cutTitleLength );
330- } else if (noteContent .length () > 0 ){
331- noteTitle = noteContent ;
337+ try {
338+ Group group = groupDao .queryGroupByName (myGroupName );
339+ if (group != null ) {
340+ if (noteTitle .length () == 0 ){//如果标题为空,则截取内容为标题
341+ if (noteContent .length () > cutTitleLength ){
342+ noteTitle = noteContent .substring (0 ,cutTitleLength );
343+ } else if (noteContent .length () > 0 ){
344+ noteTitle = noteContent ;
345+ }
332346 }
333- }
334- int groupId = group .getId ();
335- note .setTitle (noteTitle );
336- note .setContent (noteContent );
337- note .setGroupId (groupId );
338- note .setGroupName (groupName );
339- note .setType (2 );
340- note .setBgColor ("#FFFFFF" );
341- note .setIsEncrypt (0 );
342- note .setCreateTime (CommonUtil .date2string (new Date ()));
343- if (flag == 0 ) {//新建笔记
344- if (noteTitle .length () == 0 && noteContent .length () == 0 ) {
345- if (!isBackground ){
346- Toast .makeText (NewActivity .this , "请输入内容" , Toast .LENGTH_SHORT ).show ();
347+ int groupId = group .getId ();
348+ note .setTitle (noteTitle );
349+ note .setContent (noteContent );
350+ note .setGroupId (groupId );
351+ note .setGroupName (groupName );
352+ note .setType (2 );
353+ note .setBgColor ("#FFFFFF" );
354+ note .setIsEncrypt (0 );
355+ note .setCreateTime (CommonUtil .date2string (new Date ()));
356+ if (flag == 0 ) {//新建笔记
357+ if (noteTitle .length () == 0 && noteContent .length () == 0 ) {
358+ if (!isBackground ){
359+ Toast .makeText (NewActivity .this , "请输入内容" , Toast .LENGTH_SHORT ).show ();
360+ }
361+ } else {
362+ long noteId = noteDao .insertNote (note );
363+ //Log.i("", "noteId: "+noteId);
364+ //查询新建笔记id,防止重复插入
365+ note .setId ((int ) noteId );
366+ flag = 1 ;//插入以后只能是编辑
367+ if (!isBackground ){
368+ Intent intent = new Intent ();
369+ setResult (RESULT_OK , intent );
370+ finish ();
371+ }
372+ }
373+ }else if (flag == 1 ) {//编辑笔记
374+ if (!noteTitle .equals (myTitle ) || !noteContent .equals (myContent )
375+ || !groupName .equals (myGroupName ) || !noteTime .equals (myNoteTime )) {
376+ noteDao .updateNote (note );
347377 }
348- } else {
349- long noteId = noteDao .insertNote (note );
350- //Log.i("", "noteId: "+noteId);
351- //查询新建笔记id,防止重复插入
352- note .setId ((int ) noteId );
353- flag = 1 ;//插入以后只能是编辑
354378 if (!isBackground ){
355- Intent intent = new Intent ();
356- setResult (RESULT_OK , intent );
357379 finish ();
358380 }
359381 }
360- }else if (flag == 1 ) {//编辑笔记
361- if (!noteTitle .equals (myTitle ) || !noteContent .equals (myContent )
362- || !groupName .equals (myGroupName ) || !noteTime .equals (myNoteTime )) {
363- noteDao .updateNote (note );
364- }
365- if (!isBackground ){
366- finish ();
367- }
368382 }
383+ } catch (Exception e ) {
384+ e .printStackTrace ();
369385 }
370386 }
371387
@@ -435,7 +451,6 @@ protected void onActivityResult(int requestCode, int resultCode, final Intent da
435451
436452 /**
437453 * 异步方式插入图片
438- * @param data
439454 */
440455 private void insertImagesSync (final Intent data ){
441456 insertDialog .show ();
@@ -457,8 +472,8 @@ public void subscribe(ObservableEmitter<String> emitter) {
457472 emitter .onNext (imagePath );
458473 }
459474
460- // 测试插入网络图片 http://p695w3yko.bkt.clouddn .com/18-5-5/44849367 .jpg
461- //subscriber .onNext("http://p695w3yko.bkt.clouddn .com/18-5-5/30271511 .jpg");
475+ // 测试插入网络图片 http://pics.sc.chinaz .com/files/pic/pic9/201904/zzpic17414 .jpg
476+ emitter .onNext ("http://pics.sc.chinaz .com/files/pic/pic9/201903/zzpic16838 .jpg" );
462477
463478 emitter .onComplete ();
464479 }catch (Exception e ){
0 commit comments