首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android 記事本圖文儲存:佔位符分隔符方案與 Bitmap 還原

     編輯:程式設計綜合問答

同 32627,核心方案:**圖片存路徑,文字存佔位符,讀取時正則替換為 ImageSpan**。

儲存關鍵碼

// 插入圖片時
String placeholder = "[[IMG:" + imagePath + "]]";
editText.getEditableText().insert(cursorPosition, placeholder);

// 儲存
String content = editText.getText().toString();
db.insertNote(title, content);

讀取還原關鍵碼

String content = cursor.getString(contentIdx);
SpannableStringBuilder ssb = new SpannableStringBuilder(content);
Matcher m = Pattern.compile("\\[\\[IMG:(.+?)\\]\\]").matcher(content);
while (m.find()) {
    String path = m.group(1);
    Bitmap bmp = BitmapFactory.decodeFile(path);
    if (bmp != null) {
        Drawable d = new BitmapDrawable(getResources(), bmp);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ssb.setSpan(new ImageSpan(d), m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
editText.setText(ssb);

分隔符設計建議

  • 唯一且不衝突:[[IMG:...]]、###IMG###。
  • 支援屬性擴展:[[IMG:path:width:height]]。

此方案簡單、無需額外表、適合輕量級記事本 App。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved