|
|
|
@ -15,12 +15,12 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
|
import android.app.AlertDialog.Builder;
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
import android.app.AlertDialog.Builder;
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.app.SearchManager;
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
@ -54,6 +54,19 @@ import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import android.text.Spanned;
|
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
|
import android.text.Editable;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.TextNote;
|
|
|
|
@ -67,7 +80,8 @@ import net.micode.notes.ui.NoteEditText.OnTextViewChangeListener;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_2x;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_4x;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.ui.text;
|
|
|
|
|
import net.micode.notes.tool.ImageUtils;
|
|
|
|
|
import net.micode.notes.tool.ScreenUtils;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
@ -86,11 +100,10 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
public TextView tvAlertDate;
|
|
|
|
|
|
|
|
|
|
public ImageView ibSetBgColor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW);
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED);
|
|
|
|
@ -100,6 +113,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select);
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select);
|
|
|
|
@ -109,6 +123,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE);
|
|
|
|
|
sFontSizeBtnsMap.put(R.id.ll_font_small, ResourceParser.TEXT_SMALL);
|
|
|
|
@ -117,6 +132,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select);
|
|
|
|
|
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SMALL, R.id.iv_small_select);
|
|
|
|
@ -155,14 +171,6 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
private String mUserQuery;
|
|
|
|
|
private Pattern mPattern;
|
|
|
|
|
|
|
|
|
|
private text t;
|
|
|
|
|
|
|
|
|
|
public void settext(String str1){
|
|
|
|
|
t.setStr(str1);
|
|
|
|
|
}
|
|
|
|
|
public String Gettext(){
|
|
|
|
|
return t.GetStr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
@ -229,7 +237,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
getWindow().setSoftInputMode(
|
|
|
|
|
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
|
|
|
|
|
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
|
|
|
|
|
} else if(TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) {
|
|
|
|
|
} else if (TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) {
|
|
|
|
|
// New note
|
|
|
|
|
long folderId = intent.getLongExtra(Notes.INTENT_EXTRA_FOLDER_ID, 0);
|
|
|
|
|
int widgetId = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_ID,
|
|
|
|
@ -281,6 +289,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
super.onResume();
|
|
|
|
|
initNoteScreen();
|
|
|
|
|
initImage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initNoteScreen() {
|
|
|
|
@ -291,7 +300,6 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
final EditText inputServer = (EditText) view.findViewById(R.id.et_foler_name);
|
|
|
|
|
inputServer.setText("");
|
|
|
|
|
inputServer.setHint("请输入密码");
|
|
|
|
|
;
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("密码校验");
|
|
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_info).setView(inputServer);
|
|
|
|
|
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
|
|
|
|
@ -337,10 +345,41 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
* is not ready
|
|
|
|
|
*/
|
|
|
|
|
showAlertHeader();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initImage() {
|
|
|
|
|
String input = String.valueOf(mWorkingNote.getContent());
|
|
|
|
|
if(input!="null") {//创建新便签时,input的值是“null”,必须判断,不然创建的新便签都会有一个null的字符串
|
|
|
|
|
Pattern p = Pattern.compile("\\<img src=\".*?\"\\/>");
|
|
|
|
|
Matcher m = p.matcher(input);
|
|
|
|
|
SpannableString spannable = new SpannableString(input);
|
|
|
|
|
while (m.find()) {
|
|
|
|
|
String s = m.group();
|
|
|
|
|
int start = m.start();
|
|
|
|
|
int end = m.end();
|
|
|
|
|
String path = s.replaceAll("\\<img src=\"|\"\\/>", "").trim();//得到路径
|
|
|
|
|
|
|
|
|
|
int width = ScreenUtils.getScreenWidth(NoteEditActivity.this);//屏幕的宽度
|
|
|
|
|
//int height = ScreenUtils.getScreenHeight(NoteEditActivity.this);
|
|
|
|
|
try {
|
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
|
|
|
|
|
//适应屏幕,锁定横纵比缩小图片。
|
|
|
|
|
bitmap = ImageUtils.zoomImage(bitmap, (width - 32) * 0.8,
|
|
|
|
|
bitmap.getHeight() / (bitmap.getWidth() / ((width - 32) * 0.8)));
|
|
|
|
|
ImageSpan imageSpan = new ImageSpan(NoteEditActivity.this, bitmap);
|
|
|
|
|
spannable.setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mNoteEditor.setVisibility(View.VISIBLE);
|
|
|
|
|
mNoteEditor.setText(spannable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void showAlertHeader() {
|
|
|
|
|
if (mWorkingNote.hasClockAlert()) {
|
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
@ -355,7 +394,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
} else {
|
|
|
|
|
mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE);
|
|
|
|
|
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -396,7 +436,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean inRangeOfView(View view, MotionEvent ev) {
|
|
|
|
|
int []location = new int[2];
|
|
|
|
|
int[] location = new int[2];
|
|
|
|
|
view.getLocationOnScreen(location);
|
|
|
|
|
int x = location[0];
|
|
|
|
|
int y = location[1];
|
|
|
|
@ -404,8 +444,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
|| ev.getX() > (x + view.getWidth())
|
|
|
|
|
|| ev.getY() < y
|
|
|
|
|
|| ev.getY() > (y + view.getHeight())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -429,7 +469,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
for (int id : sFontSizeBtnsMap.keySet()) {
|
|
|
|
|
View view = findViewById(id);
|
|
|
|
|
view.setOnClickListener(this);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
mFontSizeId = mSharedPrefs.getInt(PREFERENCE_FONT_SIZE, ResourceParser.BG_DEFAULT_FONT_SIZE);
|
|
|
|
|
/**
|
|
|
|
@ -437,7 +478,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
* The id may larger than the length of resources, in this case,
|
|
|
|
|
* return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
|
|
|
|
|
*/
|
|
|
|
|
if(mFontSizeId >= TextAppearanceResources.getResourcesSize()) {
|
|
|
|
|
if (mFontSizeId >= TextAppearanceResources.getResourcesSize()) {
|
|
|
|
|
mFontSizeId = ResourceParser.BG_DEFAULT_FONT_SIZE;
|
|
|
|
|
}
|
|
|
|
|
mEditTextList = (LinearLayout) findViewById(R.id.note_edit_list);
|
|
|
|
@ -446,7 +487,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPause() {
|
|
|
|
|
super.onPause();
|
|
|
|
|
if(saveNote()) {
|
|
|
|
|
if (saveNote()) {
|
|
|
|
|
Log.d(TAG, "Note data was saved with length:" + mWorkingNote.getContent().length());
|
|
|
|
|
}
|
|
|
|
|
clearSettingState();
|
|
|
|
@ -463,8 +504,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {
|
|
|
|
|
mWorkingNote.getWidgetId()
|
|
|
|
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{
|
|
|
|
|
mWorkingNote.getWidgetId()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sendBroadcast(intent);
|
|
|
|
@ -476,7 +517,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
if (id == R.id.btn_set_bg_color) {
|
|
|
|
|
mNoteBgColorSelector.setVisibility(View.VISIBLE);
|
|
|
|
|
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
|
|
|
|
|
View.VISIBLE);
|
|
|
|
|
View.VISIBLE);
|
|
|
|
|
} else if (sBgSelectorBtnsMap.containsKey(id)) {
|
|
|
|
|
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
|
|
|
|
|
View.GONE);
|
|
|
|
@ -500,7 +541,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBackPressed() {
|
|
|
|
|
if(clearSettingState()) {
|
|
|
|
|
if (clearSettingState()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -525,6 +566,24 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId());
|
|
|
|
|
mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId());
|
|
|
|
|
}
|
|
|
|
|
private void encoding(){
|
|
|
|
|
//final EditText inputServer = new EditText(this);
|
|
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null);
|
|
|
|
|
//编辑Text
|
|
|
|
|
final EditText inputServer = (EditText) view.findViewById(R.id.et_foler_name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("请输入密钥");
|
|
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_info).setView(inputServer).setNegativeButton("Cancel", null);
|
|
|
|
|
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
String string = inputServer.getText().toString();
|
|
|
|
|
mWorkingNote.setPassword(string);
|
|
|
|
|
}
|
|
|
|
|
} ).show();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
@ -551,27 +610,6 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void encoding(){
|
|
|
|
|
//final EditText inputServer = new EditText(this);
|
|
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null);
|
|
|
|
|
//编辑Text
|
|
|
|
|
final EditText inputServer = (EditText) view.findViewById(R.id.et_foler_name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("请输入密钥");
|
|
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_info).setView(inputServer).setNegativeButton("Cancel", null);
|
|
|
|
|
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
String string = inputServer.getText().toString();
|
|
|
|
|
mWorkingNote.setPassword(string);
|
|
|
|
|
}
|
|
|
|
|
} ).show();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
@ -597,6 +635,11 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
mFontSizeSelector.setVisibility(View.VISIBLE);
|
|
|
|
|
findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.VISIBLE);
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_Insert_Image:
|
|
|
|
|
Intent albumIntent = new Intent(Intent.ACTION_PICK);
|
|
|
|
|
albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
|
|
|
|
|
startActivityForResult(albumIntent, 1);
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_list_mode:
|
|
|
|
|
mWorkingNote.setCheckListMode(mWorkingNote.getCheckListMode() == 0 ?
|
|
|
|
|
TextNote.MODE_CHECK_LIST : 0);
|
|
|
|
@ -627,7 +670,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
DateTimePickerDialog d = new DateTimePickerDialog(this, System.currentTimeMillis());
|
|
|
|
|
d.setOnDateTimeSetListener(new OnDateTimeSetListener() {
|
|
|
|
|
public void OnDateTimeSet(AlertDialog dialog, long date) {
|
|
|
|
|
mWorkingNote.setAlertDate(date , true);
|
|
|
|
|
mWorkingNote.setAlertDate(date, true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
d.show();
|
|
|
|
@ -696,7 +739,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
|
|
|
|
|
AlarmManager alarmManager = ((AlarmManager) getSystemService(ALARM_SERVICE));
|
|
|
|
|
showAlertHeader();
|
|
|
|
|
if(!set) {
|
|
|
|
|
if (!set) {
|
|
|
|
|
alarmManager.cancel(pendingIntent);
|
|
|
|
|
} else {
|
|
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent);
|
|
|
|
@ -729,7 +772,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
|
|
|
|
|
mEditTextList.removeViewAt(index);
|
|
|
|
|
NoteEditText edit = null;
|
|
|
|
|
if(index == 0) {
|
|
|
|
|
if (index == 0) {
|
|
|
|
|
edit = (NoteEditText) mEditTextList.getChildAt(0).findViewById(
|
|
|
|
|
R.id.et_edit_text);
|
|
|
|
|
} else {
|
|
|
|
@ -746,7 +789,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
/**
|
|
|
|
|
* Should not happen, check for debug
|
|
|
|
|
*/
|
|
|
|
|
if(index > mEditTextList.getChildCount()) {
|
|
|
|
|
if (index > mEditTextList.getChildCount()) {
|
|
|
|
|
Log.e(TAG, "Index out of mEditTextList boundrary, should not happen");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -766,7 +809,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
String[] items = text.split("\n");
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (String item : items) {
|
|
|
|
|
if(!TextUtils.isEmpty(item)) {
|
|
|
|
|
if (!TextUtils.isEmpty(item)) {
|
|
|
|
|
mEditTextList.addView(getListItem(item, index));
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
@ -831,7 +874,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
Log.e(TAG, "Wrong index, should not happen");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(hasText) {
|
|
|
|
|
if (hasText) {
|
|
|
|
|
mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.VISIBLE);
|
|
|
|
|
} else {
|
|
|
|
|
mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.GONE);
|
|
|
|
@ -940,4 +983,92 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
private void showToast(int resId, int duration) {
|
|
|
|
|
Toast.makeText(this, resId, duration).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void callGallery() {
|
|
|
|
|
Intent albumIntent = new Intent(Intent.ACTION_PICK);
|
|
|
|
|
albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
|
|
|
|
|
startActivityForResult(albumIntent, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {//处理从图库中返回的图片。
|
|
|
|
|
|
|
|
|
|
if (requestCode == 1) {//callGallery()方法是以请求码为1来开启活动的,此处为响应活动结束
|
|
|
|
|
try {
|
|
|
|
|
// 获得图片的资源标识符
|
|
|
|
|
Uri originalUri = data.getData();
|
|
|
|
|
String[] proj = {MediaStore.Images.Media.DATA};
|
|
|
|
|
//从数据库中查询
|
|
|
|
|
Cursor cursor = getContentResolver().query(originalUri, proj, null, null, null);
|
|
|
|
|
//获得用户选择的图片的索引值
|
|
|
|
|
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
|
|
|
// 将光标移至开头 ,这个很重要,不小心很容易引起越界
|
|
|
|
|
cursor.moveToFirst();
|
|
|
|
|
// 最后根据索引值获取图片路径
|
|
|
|
|
String path = cursor.getString(column_index);
|
|
|
|
|
//插入图片
|
|
|
|
|
insertImg(path);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
Toast.makeText(NoteEditActivity.this, "图片插入失败", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private SpannableString getBitmapMime(String path, String tagPath) {
|
|
|
|
|
SpannableString ss = new SpannableString(tagPath);//这里使用加了<img>标签的图片路径
|
|
|
|
|
int width = ScreenUtils.getScreenWidth(NoteEditActivity.this);
|
|
|
|
|
//int height = ScreenUtils.getScreenHeight(NoteEditActivity.this);
|
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
|
//路径对应的图片
|
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
|
|
|
|
|
//适应屏幕,锁定横纵比缩小图片。
|
|
|
|
|
bitmap = ImageUtils.zoomImage(bitmap, (width - 32) * 0.8,
|
|
|
|
|
bitmap.getHeight() / (bitmap.getWidth() / ((width - 32) * 0.8)));
|
|
|
|
|
ImageSpan imageSpan = new ImageSpan(this, bitmap);
|
|
|
|
|
ss.setSpan(imageSpan, 0, tagPath.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
return ss;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void insertImg(String path) {
|
|
|
|
|
String tagPath = "<img src=\"" + path + "\"/>";//为图片路径加上<img>标签
|
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeFile(path);//根据路径找到图片
|
|
|
|
|
if (bitmap != null) {
|
|
|
|
|
//格式化存储路径
|
|
|
|
|
SpannableString ss = getBitmapMime(path, tagPath);
|
|
|
|
|
//将图片插入文本视图
|
|
|
|
|
insertPhotoToEditText(ss);
|
|
|
|
|
Log.d("insertimage", path + "与" + tagPath);
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(NoteEditActivity.this, "插入失败,无读写存储权限,请到权限中心开启", Toast.LENGTH_LONG).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void insertPhotoToEditText(SpannableString ss) {//把文本插入便签,如
|
|
|
|
|
Editable et = mNoteEditor.getText();
|
|
|
|
|
int start = mNoteEditor.getSelectionStart();
|
|
|
|
|
ss.getSpans(0, ss.length(), ImageSpan.class);
|
|
|
|
|
// 将图片添加进EditText中
|
|
|
|
|
if(start < 0 || start >= et.length()){
|
|
|
|
|
//如果是在文本的结尾
|
|
|
|
|
et.append(ss);
|
|
|
|
|
}else{
|
|
|
|
|
//如果是在文本的中间
|
|
|
|
|
et.insert(start, ss);
|
|
|
|
|
}
|
|
|
|
|
//将图片路径写入数据库——非常关键,否则无法显示图片
|
|
|
|
|
mWorkingNote.setWorkingText(et+"\n");
|
|
|
|
|
mNoteEditor.setText(et);
|
|
|
|
|
mNoteEditor.setSelection(start + ss.length());
|
|
|
|
|
mNoteEditor.setFocusableInTouchMode(true);
|
|
|
|
|
mNoteEditor.setFocusable(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|