|
|
|
|
@ -27,18 +27,26 @@ import android.content.Context;
|
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
|
import android.text.Spannable;
|
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.Editable;
|
|
|
|
|
import android.text.Html;
|
|
|
|
|
import android.text.format.DateUtils;
|
|
|
|
|
import android.text.style.BackgroundColorSpan;
|
|
|
|
|
import android.text.style.StyleSpan;
|
|
|
|
|
import android.text.style.UnderlineSpan;
|
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
@ -51,23 +59,19 @@ import android.widget.CheckBox;
|
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
|
import android.text.Editable;
|
|
|
|
|
import android.text.style.ImageSpan;
|
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
// import android.support.annotation.NonNull;
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
@ -172,7 +176,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
private SharedPreferences mSharedPrefs; // 共享偏好设置
|
|
|
|
|
private int mFontSizeId; // 字体大小ID
|
|
|
|
|
private ImageButton mBtnInsertImage; // 插入图片按钮 OMO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 富文本工具栏按钮
|
|
|
|
|
private ImageButton mBtnBold; // 加粗按钮
|
|
|
|
|
private ImageButton mBtnItalic; // 斜体按钮
|
|
|
|
|
@ -364,79 +368,22 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
mNoteEditor.setText("");
|
|
|
|
|
mNoteEditor.setSelection(0);
|
|
|
|
|
} else {
|
|
|
|
|
Spannable spannable;
|
|
|
|
|
|
|
|
|
|
// 检查是否是HTML格式(包含HTML标签)
|
|
|
|
|
if (content.contains("<") && content.contains(">")) {
|
|
|
|
|
try {
|
|
|
|
|
// 从HTML格式恢复SpannableString,保留样式(加粗、斜体、下划线)
|
|
|
|
|
// Html.fromHtml会自动将HTML标签转换为对应的Span
|
|
|
|
|
spannable = (Spannable) Html.fromHtml(content);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 如果HTML解析失败,使用普通文本
|
|
|
|
|
Log.e(TAG, "Error parsing HTML content: " + e.getMessage());
|
|
|
|
|
spannable = new SpannableString(content);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 普通文本格式,直接创建SpannableString
|
|
|
|
|
spannable = new SpannableString(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理图片:将包含[IMAGE]标签的文本转换为包含ImageSpan的SpannableString
|
|
|
|
|
// 注意:convertTextToSpannableWithImages会处理[IMAGE]标记,但会保留文本内容
|
|
|
|
|
String textContent = spannable.toString();
|
|
|
|
|
SpannableString finalSpannable = convertTextToSpannableWithImages(textContent);
|
|
|
|
|
|
|
|
|
|
// 将样式从原始Spannable复制到处理后的SpannableString
|
|
|
|
|
// 由于convertTextToSpannableWithImages可能改变了文本长度(用空格替换了[IMAGE]标签),
|
|
|
|
|
// 我们需要基于文本内容来映射样式位置
|
|
|
|
|
if (textContent.length() == finalSpannable.length()) {
|
|
|
|
|
// 文本长度相同,直接复制样式
|
|
|
|
|
StyleSpan[] styleSpans = spannable.getSpans(0, spannable.length(), StyleSpan.class);
|
|
|
|
|
UnderlineSpan[] underlineSpans = spannable.getSpans(0, spannable.length(), UnderlineSpan.class);
|
|
|
|
|
|
|
|
|
|
for (StyleSpan span : styleSpans) {
|
|
|
|
|
int start = spannable.getSpanStart(span);
|
|
|
|
|
int end = spannable.getSpanEnd(span);
|
|
|
|
|
if (start >= 0 && end <= finalSpannable.length() && start < end) {
|
|
|
|
|
finalSpannable.setSpan(new StyleSpan(span.getStyle()), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (UnderlineSpan span : underlineSpans) {
|
|
|
|
|
int start = spannable.getSpanStart(span);
|
|
|
|
|
int end = spannable.getSpanEnd(span);
|
|
|
|
|
if (start >= 0 && end <= finalSpannable.length() && start < end) {
|
|
|
|
|
finalSpannable.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 文本长度不同,说明有图片被处理了
|
|
|
|
|
// 这种情况下,样式位置需要重新计算
|
|
|
|
|
// 为了简化,我们基于原始文本位置映射样式(可能会有偏差,但应该能处理大部分情况)
|
|
|
|
|
StyleSpan[] styleSpans = spannable.getSpans(0, spannable.length(), StyleSpan.class);
|
|
|
|
|
UnderlineSpan[] underlineSpans = spannable.getSpans(0, spannable.length(), UnderlineSpan.class);
|
|
|
|
|
|
|
|
|
|
for (StyleSpan span : styleSpans) {
|
|
|
|
|
int start = Math.max(0, Math.min(spannable.getSpanStart(span), finalSpannable.length()));
|
|
|
|
|
int end = Math.max(start, Math.min(spannable.getSpanEnd(span), finalSpannable.length()));
|
|
|
|
|
if (start < end) {
|
|
|
|
|
finalSpannable.setSpan(new StyleSpan(span.getStyle()), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (UnderlineSpan span : underlineSpans) {
|
|
|
|
|
int start = Math.max(0, Math.min(spannable.getSpanStart(span), finalSpannable.length()));
|
|
|
|
|
int end = Math.max(start, Math.min(spannable.getSpanEnd(span), finalSpannable.length()));
|
|
|
|
|
if (start < end) {
|
|
|
|
|
finalSpannable.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 第一步:处理HTML编码字符,将&#xxx;转换为实际字符
|
|
|
|
|
String decodedContent = content;
|
|
|
|
|
try {
|
|
|
|
|
// 使用Html.fromHtml解码HTML编码字符
|
|
|
|
|
decodedContent = Html.fromHtml(content).toString();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error decoding HTML content: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mNoteEditor.setText(finalSpannable);
|
|
|
|
|
|
|
|
|
|
// 第二步:处理[IMAGE]标签,将其转换为ImageSpan
|
|
|
|
|
SpannableString spannable = convertTextToSpannableWithImages(decodedContent);
|
|
|
|
|
|
|
|
|
|
// 第三步:设置文本,确保文字和图片都能正常显示
|
|
|
|
|
mNoteEditor.setText(spannable);
|
|
|
|
|
// 将光标定位到文本末尾
|
|
|
|
|
mNoteEditor.setSelection(finalSpannable.length());
|
|
|
|
|
mNoteEditor.setSelection(spannable.length());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -472,6 +419,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
return new SpannableString("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Converting text to Spannable with images: " + text);
|
|
|
|
|
|
|
|
|
|
// 创建一个新的SpannableStringBuilder
|
|
|
|
|
SpannableStringBuilder builder = new SpannableStringBuilder();
|
|
|
|
|
|
|
|
|
|
@ -480,6 +429,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
int imageStart = text.indexOf("[IMAGE]", startIndex);
|
|
|
|
|
|
|
|
|
|
while (imageStart != -1) {
|
|
|
|
|
Log.d(TAG, "Found [IMAGE] tag at position: " + imageStart);
|
|
|
|
|
|
|
|
|
|
// 添加[IMAGE]标签之前的文本
|
|
|
|
|
builder.append(text.substring(startIndex, imageStart));
|
|
|
|
|
|
|
|
|
|
@ -491,43 +442,97 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Found [/IMAGE] tag at position: " + imageEnd);
|
|
|
|
|
|
|
|
|
|
// 提取图片URI
|
|
|
|
|
String imageUri = text.substring(imageStart + "[IMAGE]".length(), imageEnd);
|
|
|
|
|
Log.d(TAG, "Found image URI: " + imageUri);
|
|
|
|
|
Log.d(TAG, "Extracted image URI: " + imageUri);
|
|
|
|
|
|
|
|
|
|
// 创建一个占位符字符串,用于插入ImageSpan
|
|
|
|
|
String placeholder = " ";
|
|
|
|
|
int placeholderStart = builder.length();
|
|
|
|
|
builder.append(placeholder);
|
|
|
|
|
// 保留原始的[IMAGE]uri[/IMAGE]标签,直接在上面添加ImageSpan
|
|
|
|
|
String fullImageTag = "[IMAGE]" + imageUri + "[/IMAGE]";
|
|
|
|
|
int tagStart = builder.length();
|
|
|
|
|
builder.append(fullImageTag);
|
|
|
|
|
|
|
|
|
|
// 尝试将图片URI转换为Drawable并创建ImageSpan
|
|
|
|
|
try {
|
|
|
|
|
// 确保URI是有效的
|
|
|
|
|
Uri uri = Uri.parse(imageUri);
|
|
|
|
|
if (uri == null) {
|
|
|
|
|
Log.e(TAG, "Invalid URI: " + imageUri);
|
|
|
|
|
// 更新索引,继续处理下一个标签
|
|
|
|
|
startIndex = imageEnd + "[/IMAGE]".length();
|
|
|
|
|
imageStart = text.indexOf("[IMAGE]", startIndex);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Attempting to load image from URI: " + uri.toString());
|
|
|
|
|
|
|
|
|
|
// 使用BitmapFactory直接加载图片,这是最可靠的方式
|
|
|
|
|
ContentResolver resolver = getContentResolver();
|
|
|
|
|
InputStream inputStream = resolver.openInputStream(Uri.parse(imageUri));
|
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
|
|
|
|
inputStream.close();
|
|
|
|
|
|
|
|
|
|
// 计算图片的合适大小,确保它能适应EditText的宽度
|
|
|
|
|
int screenWidth = getResources().getDisplayMetrics().widthPixels;
|
|
|
|
|
int imageWidth = bitmap.getWidth();
|
|
|
|
|
int imageHeight = bitmap.getHeight();
|
|
|
|
|
float scale = (float) screenWidth / (float) imageWidth;
|
|
|
|
|
int scaledHeight = (int) (imageHeight * scale);
|
|
|
|
|
|
|
|
|
|
// 缩放图片
|
|
|
|
|
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, screenWidth, scaledHeight, true);
|
|
|
|
|
Drawable drawable = new android.graphics.drawable.BitmapDrawable(getResources(), scaledBitmap);
|
|
|
|
|
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
|
|
|
|
|
|
|
|
|
// 创建ImageSpan并添加到SpannableStringBuilder中
|
|
|
|
|
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
|
|
|
|
|
builder.setSpan(imageSpan, placeholderStart, placeholderStart + placeholder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
|
InputStream inputStream = null;
|
|
|
|
|
try {
|
|
|
|
|
inputStream = resolver.openInputStream(uri);
|
|
|
|
|
if (inputStream == null) {
|
|
|
|
|
Log.e(TAG, "Failed to open input stream for URI: " + uri.toString());
|
|
|
|
|
// 更新索引,继续处理下一个标签
|
|
|
|
|
startIndex = imageEnd + "[/IMAGE]".length();
|
|
|
|
|
imageStart = text.indexOf("[IMAGE]", startIndex);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解码图片
|
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
|
|
|
|
if (bitmap == null) {
|
|
|
|
|
Log.e(TAG, "Failed to decode bitmap from URI: " + uri.toString());
|
|
|
|
|
// 更新索引,继续处理下一个标签
|
|
|
|
|
startIndex = imageEnd + "[/IMAGE]".length();
|
|
|
|
|
imageStart = text.indexOf("[IMAGE]", startIndex);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Successfully decoded bitmap with dimensions: " + bitmap.getWidth() + "x" + bitmap.getHeight());
|
|
|
|
|
|
|
|
|
|
// 计算图片的合适大小,确保它能适应EditText的宽度
|
|
|
|
|
int screenWidth = getResources().getDisplayMetrics().widthPixels;
|
|
|
|
|
// 减去左右边距,确保图片能完整显示
|
|
|
|
|
int maxWidth = screenWidth - 40;
|
|
|
|
|
int imageWidth = bitmap.getWidth();
|
|
|
|
|
int imageHeight = bitmap.getHeight();
|
|
|
|
|
|
|
|
|
|
// 计算缩放比例
|
|
|
|
|
float scale = 1.0f;
|
|
|
|
|
if (imageWidth > maxWidth) {
|
|
|
|
|
scale = (float) maxWidth / (float) imageWidth;
|
|
|
|
|
}
|
|
|
|
|
Log.d(TAG, "Calculated scale: " + scale);
|
|
|
|
|
|
|
|
|
|
// 缩放图片(如果需要)
|
|
|
|
|
if (scale < 1.0f) {
|
|
|
|
|
bitmap = Bitmap.createScaledBitmap(bitmap, (int)(imageWidth * scale), (int)(imageHeight * scale), true);
|
|
|
|
|
Log.d(TAG, "Scaled bitmap to: " + bitmap.getWidth() + "x" + bitmap.getHeight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建Drawable并设置边界
|
|
|
|
|
Drawable drawable = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);
|
|
|
|
|
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
|
|
|
|
|
|
|
|
|
// 创建ImageSpan并添加到SpannableStringBuilder中
|
|
|
|
|
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
|
|
|
|
|
builder.setSpan(imageSpan, tagStart, tagStart + fullImageTag.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
|
Log.d(TAG, "Successfully added ImageSpan for URI: " + imageUri);
|
|
|
|
|
} finally {
|
|
|
|
|
// 确保InputStream被正确关闭
|
|
|
|
|
if (inputStream != null) {
|
|
|
|
|
try {
|
|
|
|
|
inputStream.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error closing input stream: " + e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error loading image: " + e.toString());
|
|
|
|
|
// 如果加载图片失败,直接显示图片URI
|
|
|
|
|
builder.append("[IMAGE]");
|
|
|
|
|
builder.append(imageUri);
|
|
|
|
|
builder.append("[/IMAGE]");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新索引,继续查找下一个[IMAGE]标签
|
|
|
|
|
@ -538,6 +543,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
// 添加剩余的文本
|
|
|
|
|
builder.append(text.substring(startIndex));
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Final SpannableStringBuilder content: " + builder.toString());
|
|
|
|
|
return new SpannableString(builder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -679,12 +685,12 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
if (mBtnInsertImage != null) {
|
|
|
|
|
mBtnInsertImage.setOnClickListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化富文本工具栏按钮
|
|
|
|
|
mBtnBold = (ImageButton) findViewById(R.id.btn_bold);
|
|
|
|
|
mBtnItalic = (ImageButton) findViewById(R.id.btn_italic);
|
|
|
|
|
mBtnUnderline = (ImageButton) findViewById(R.id.btn_underline);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 为富文本工具栏按钮设置点击监听器
|
|
|
|
|
if (mBtnBold != null) {
|
|
|
|
|
mBtnBold.setOnClickListener(this);
|
|
|
|
|
@ -1266,16 +1272,10 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
mWorkingNote.setWorkingText(sb.toString());
|
|
|
|
|
} else {
|
|
|
|
|
// 保存时,将SpannableString转换为HTML格式以保留样式信息
|
|
|
|
|
Spannable spannable = mNoteEditor.getText();
|
|
|
|
|
if (spannable != null && spannable.length() > 0) {
|
|
|
|
|
// 将Spannable转换为HTML格式,保留加粗、斜体、下划线等样式
|
|
|
|
|
// Html.toHtml会自动处理StyleSpan和UnderlineSpan
|
|
|
|
|
String htmlContent = Html.toHtml(spannable);
|
|
|
|
|
mWorkingNote.setWorkingText(htmlContent);
|
|
|
|
|
} else {
|
|
|
|
|
mWorkingNote.setWorkingText("");
|
|
|
|
|
}
|
|
|
|
|
// 对于普通模式,直接保存EditText中的原始文本
|
|
|
|
|
// 这样可以保留[IMAGE]标签,确保图片信息不会丢失
|
|
|
|
|
String content = mNoteEditor.getText().toString();
|
|
|
|
|
mWorkingNote.setWorkingText(content);
|
|
|
|
|
}
|
|
|
|
|
return hasChecked;
|
|
|
|
|
}
|
|
|
|
|
@ -1372,9 +1372,39 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
/**
|
|
|
|
|
* 从相册选择图片 OMO
|
|
|
|
|
*/
|
|
|
|
|
private static final int REQUEST_CODE_PERMISSION_STORAGE = 101;
|
|
|
|
|
|
|
|
|
|
private void pickImageFromGallery() {
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
|
|
|
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
|
|
|
|
|
// 检查并申请存储权限
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// 申请权限
|
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION_STORAGE);
|
|
|
|
|
} else {
|
|
|
|
|
// 权限已授予,直接选择图片
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
|
|
|
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Android 6.0以下版本,直接选择图片
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
|
|
|
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理权限请求结果
|
|
|
|
|
@Override
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
if (requestCode == REQUEST_CODE_PERMISSION_STORAGE) {
|
|
|
|
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// 权限被授予,执行图片选择
|
|
|
|
|
pickImageFromGallery();
|
|
|
|
|
} else {
|
|
|
|
|
// 权限被拒绝,显示提示
|
|
|
|
|
showToast(R.string.error_permission_denied);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -1388,27 +1418,50 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
Uri imageUri = data.getData();
|
|
|
|
|
if (imageUri != null) {
|
|
|
|
|
Log.d(TAG, "Image URI: " + imageUri.toString());
|
|
|
|
|
|
|
|
|
|
// 获取当前编辑器内容
|
|
|
|
|
Editable editable = mNoteEditor.getText();
|
|
|
|
|
int currentPosition = mNoteEditor.getSelectionStart();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 将图片路径添加到笔记内容中
|
|
|
|
|
String currentContent = mNoteEditor.getText().toString();
|
|
|
|
|
// 1. 保存当前编辑器的原始内容,用于后续恢复
|
|
|
|
|
String originalContent = editable.toString();
|
|
|
|
|
int originalSelection = mNoteEditor.getSelectionStart();
|
|
|
|
|
|
|
|
|
|
// 2. 插入完整的[IMAGE]uri[/IMAGE]标签到原始内容中
|
|
|
|
|
String imagePath = "[IMAGE]" + imageUri.toString() + "[/IMAGE]";
|
|
|
|
|
String newContent = currentContent + (currentContent.isEmpty() ? "" : "\n") + imagePath;
|
|
|
|
|
Log.d(TAG, "New content: " + newContent);
|
|
|
|
|
StringBuilder sb = new StringBuilder(originalContent);
|
|
|
|
|
sb.insert(originalSelection, imagePath);
|
|
|
|
|
String newContentWithTags = sb.toString();
|
|
|
|
|
|
|
|
|
|
// 3. 首先将包含ImageSpan的SpannableString显示到编辑器中
|
|
|
|
|
SpannableString spannable = convertTextToSpannableWithImages(newContentWithTags);
|
|
|
|
|
mNoteEditor.setText(spannable);
|
|
|
|
|
|
|
|
|
|
// 4. 然后将原始的[IMAGE]标签内容设置到WorkingNote中
|
|
|
|
|
mWorkingNote.setWorkingText(newContentWithTags);
|
|
|
|
|
|
|
|
|
|
// 5. 保存笔记(注意:这里不再调用getWorkingText(),而是直接保存WorkingNote中的内容)
|
|
|
|
|
// 我们需要修改saveNote()方法,或者直接调用mWorkingNote.saveNote()
|
|
|
|
|
if (mWorkingNote.saveNote()) {
|
|
|
|
|
showToast(R.string.info_image_inserted);
|
|
|
|
|
} else {
|
|
|
|
|
showToast(R.string.error_image_insert_failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 直接设置文本,不使用ImageSpan,这样可以确保内容被保存
|
|
|
|
|
mNoteEditor.setText(newContent);
|
|
|
|
|
mNoteEditor.setSelection(newContent.length());
|
|
|
|
|
// 6. 将光标移动到图片后面
|
|
|
|
|
int newPosition = originalSelection + imagePath.length();
|
|
|
|
|
mNoteEditor.setSelection(newPosition);
|
|
|
|
|
|
|
|
|
|
// 保存笔记内容,确保图片路径被正确存储
|
|
|
|
|
saveNote();
|
|
|
|
|
showToast(R.string.info_image_inserted);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error inserting image: " + e.toString());
|
|
|
|
|
showToast(R.string.error_note_not_exist);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
// 如果插入图片失败,显示错误信息
|
|
|
|
|
showToast(R.string.error_image_insert_failed);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Image URI is null");
|
|
|
|
|
showToast(R.string.error_note_not_exist);
|
|
|
|
|
showToast(R.string.error_image_insert_failed);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "Image selection canceled or failed, resultCode: " + resultCode);
|
|
|
|
|
@ -1498,38 +1551,38 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
} else {
|
|
|
|
|
// 加粗或斜体样式
|
|
|
|
|
StyleSpan[] styleSpans = editable.getSpans(selectionStart, selectionEnd, StyleSpan.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查选中范围内是否已经有该样式
|
|
|
|
|
boolean hasStyle = false;
|
|
|
|
|
int existingStyle = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 收集选中范围内的所有样式
|
|
|
|
|
for (StyleSpan span : styleSpans) {
|
|
|
|
|
int spanStart = editable.getSpanStart(span);
|
|
|
|
|
int spanEnd = editable.getSpanEnd(span);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果span与选中范围重叠
|
|
|
|
|
if (spanStart < selectionEnd && spanEnd > selectionStart) {
|
|
|
|
|
int spanStyle = span.getStyle();
|
|
|
|
|
existingStyle |= spanStyle;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否包含目标样式
|
|
|
|
|
if ((spanStyle & styleType) == styleType) {
|
|
|
|
|
hasStyle = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 移除选中范围内的所有StyleSpan,以便重新应用
|
|
|
|
|
for (int i = styleSpans.length - 1; i >= 0; i--) {
|
|
|
|
|
StyleSpan span = styleSpans[i];
|
|
|
|
|
int spanStart = editable.getSpanStart(span);
|
|
|
|
|
int spanEnd = editable.getSpanEnd(span);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果span与选中范围重叠
|
|
|
|
|
if (spanStart < selectionEnd && spanEnd > selectionStart) {
|
|
|
|
|
editable.removeSpan(span);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 处理span范围大于选中范围的情况,保留未选中部分的样式
|
|
|
|
|
int spanStyle = span.getStyle();
|
|
|
|
|
if (spanStart < selectionStart) {
|
|
|
|
|
@ -1542,7 +1595,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 切换样式:如果有则移除,如果没有则添加
|
|
|
|
|
int finalStyle = existingStyle;
|
|
|
|
|
if (hasStyle) {
|
|
|
|
|
@ -1552,7 +1605,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
// 添加目标样式
|
|
|
|
|
finalStyle |= styleType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 应用最终样式
|
|
|
|
|
if (finalStyle != 0) {
|
|
|
|
|
editable.setSpan(new StyleSpan(finalStyle), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|