You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xiaomi_notes_reading/src/notes/tool/ImageSpanUtils.java

103 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.tool;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* 图片Span工具类用于处理图片在Spannable中的序列化和反序列化
*/
public class ImageSpanUtils {
/**
* 将Spannable中的图片信息序列化为JSON字符串
*
* @param spannable 包含图片信息的Spannable对象
* @param content 原始文本内容
* @return 图片信息的JSON字符串表示
*/
public static String serializeImageInfo(Spannable spannable, String content) {
JSONArray jsonArray = new JSONArray();
// 获取所有图片Span
ImageSpan[] imageSpans = spannable.getSpans(0, spannable.length(), ImageSpan.class);
for (ImageSpan span : imageSpans) {
JSONObject spanObj = new JSONObject();
try {
int start = spannable.getSpanStart(span);
int end = spannable.getSpanEnd(span);
// 获取图片URI如果有的话
Drawable drawable = span.getDrawable();
if (drawable != null) {
// 这里我们假设图片信息已经在其他地方存储,只需记录位置和路径
// 在实际实现中,你可能需要将图片保存到特定位置并记录路径
spanObj.put("type", "image");
spanObj.put("start", start);
spanObj.put("end", end);
// 注意ImageSpan通常不直接存储URI需要额外的机制来跟踪图片路径
// 这里仅为示例,实际实现需要根据具体情况调整
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonArray.toString();
}
/**
* 将JSON格式的图片信息反序列化并应用到文本上
*
* @param context 上下文
* @param text 原始文本
* @param imageJson 序列化的图片信息JSON字符串
* @return 包含图片信息的SpannableStringBuilder
*/
public static SpannableStringBuilder deserializeImageInfo(Context context, String text, String imageJson) {
SpannableStringBuilder spannable = new SpannableStringBuilder(text);
if (imageJson == null || imageJson.isEmpty()) {
return spannable;
}
try {
// 这里需要根据实际的图片存储机制来实现
// 因为图片通常需要特殊处理,这里暂时返回原始文本
// 实际实现需要考虑如何存储和检索图片
return spannable;
} catch (Exception e) {
e.printStackTrace();
}
return spannable;
}
}