解析EditText.getText().toString()得到的图片

求助各位大神:我在EditText中插入了一张图片,我用edit_text.getText().toString将图片以String形式保存,我怎么把String又转换为图片显示在TextView中呢?

 SpannableString ss = new SpannableString(imagePath);
    Pattern p=Pattern.compile("/mnt/sdcard/.+?\\.\\w{3}");
    Matcher m=p.matcher(imagePath);
    while(m.find()){
        Bitmap bm = BitmapFactory.decodeFile(m.group());
        Bitmap rbm = r.resizeImage(bm, 100, 100);
        ImageSpan span = new ImageSpan(this, rbm);
        ss.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    Editable edit_text = display.getEditableText();
    edit_text.append(ss);

这样做只能得到路径格式的字符串。怎么让图片再显示出来呢?

忘记说了,我是把最后的ss转换成charSequence类型,再调用editText.setText(ss)。

http://blog.csdn.net/woshisap/article/details/6619449

建议你的正则写成这样:

 Pattern p = Pattern.compile("/.*(jpg|jpeg|png|bmp|gif)");

while循环里,取到照片resize,调用setSpan。最后调用editText.setText(ss)。这样就行了。

下面是我刚才测试过的:
图片说明

Activity:

 public class ShowImageActivity extends Activity {

    private EditText et_image;
    private ImageView img_show;
    private final static int REQUEST_SELECT_PICTURE = 0x11;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_image);

        et_image = (EditText) findViewById(R.id.et_image);
        img_show = (ImageView) findViewById(R.id.img_show);
        Button btn_select = (Button) findViewById(R.id.btn_select);
        btn_select.setOnClickListener(view -> doSelectPicture());
    }

    //使用Bitmap调整尺寸
    private static Drawable resizeImage(Bitmap bitmap, int newWidth, int newHeight)
    {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return new BitmapDrawable(resizedBitmap);
    }

    private CharSequence addImageWithText(String imagePath){
        SpannableString ss = new SpannableString(imagePath);
        Pattern p = Pattern.compile("/.*(jpg|jpeg|png|bmp|gif)");
        Matcher m = p.matcher(imagePath);
        while(m.find()){
            Log.e("123456", "image_path_match = " + m.group());
            Bitmap bitmap = BitmapFactory.decodeFile(m.group());
            Drawable drawable = resizeImage(bitmap, 100, 100);
            img_show.setImageDrawable(drawable);
            ImageSpan span = new ImageSpan(drawable);
            ss.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    public void doSelectPicture() {
        Intent intent;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");

        } else {
            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        }
        startActivityForResult(intent, REQUEST_SELECT_PICTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null && requestCode == REQUEST_SELECT_PICTURE){
            try {
                Uri uri = data.getData();
                String [] project={MediaStore.Images.Media.DATA};
                Cursor cursor = managedQuery( uri, project, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String path = cursor.getString(column_index);
                Log.e("123456", "image_path_real = " + path);
                CharSequence withImage = addImageWithText(path);
                et_image.setText(withImage);
            }catch (Exception e){
                Log.e("123456", e.toString());
            }
        }
    }
}

xml(其中ImagView是用来显示选择本地照片原图):

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/dp_5"
    android:background="@color/white">

    <EditText
        android:id="@+id/et_image"
        android:layout_marginTop="160dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="100dp"/>

    <Button
        android:id="@+id/btn_select"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择照片"
        android:textSize="18sp"
        android:layout_gravity="center_horizontal"/>

    <ImageView
        android:id="@+id/img_show"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

访问SD卡权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

非常感谢图片说明
辛苦了。