EditText实现图文混排,保存与解析(墨雪亲测)

1.设计思路:

       定义2个按钮,一个插入图片,一个拍照。插入图片时,从系统相册拿到图片,紧接着将拿到的图片重命名保存到指定的手机的文件夹中。然后根据路径和你刚刚设置的文件名从文件夹中获取这张图片放到EditText中,以字符串形式保存到数据库(自动变成路径+文件名),最后要从数据库中拿数据出来,以正则表达式提取路径并替换为图片。拍照也是一样的原理。

2.代码:
onCreate()方法中:

//+_+_+_+_+_+_+_+_+_+_+_+_+_+_+插入图片的按钮事件+_+_+_+_+_+_+_+_+_+_+_+
        ImageButton btn_insert=(ImageButton)findViewById(R.id.insert_photo);
        btn_insert.setOnClickListener(new Button.OnClickListener() {//插入
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
                getImage.addCategory(Intent.CATEGORY_OPENABLE);
                getImage.setType("image/*");
                startActivityForResult(getImage, 0x111);
            }
        });

        ImageButton btn_takePhoto=(ImageButton)findViewById(R.id.take_photo);
        btn_takePhoto.setOnClickListener(new Button.OnClickListener(){//拍照

            @Override
            public void onClick(View arg0) {
                // TODO 自动生成的方法存根
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                 startActivityForResult(intent, 0x222);

            }

        });
        //+_+_+_+_+_+_+_+_+_+_+_+_+_+_+插入图片的按钮事件+_+_+_+_+_+_+_+_+_+_+_+//

onActivityResult()方法中:

//+_+_+_+_+获取图片+_+_+_+_+
        ContentResolver resolver=getContentResolver();
        EditText editText=(EditText)findViewById(R.id.content);
        if(requestCode==0x111&&resultCode==RESULT_OK){//requestCode=0x111是从相册获取。
            Uri originalUri=data.getData();
            Bitmap ori_bitmap = null;
            Bitmap ori_rbitmap = null;
            try {
                ori_bitmap=BitmapFactory.decodeStream(resolver.openInputStream(originalUri));
                ori_rbitmap=resizeimg.resizeImage(ori_bitmap, 300, 300);
            } catch (FileNotFoundException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }

            String sdStatus = Environment.getExternalStorageState();  
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
                Log.i("TestFile","SD card is not avaiable/writeable right now.");                   
             } 

             String name =Calendar.getInstance(Locale.CHINA).getTimeInMillis() + ".jpg";
             FileOutputStream FOut = null;    
             File file = new File("/sdcard/myImage/");  
             file.mkdirs();// 创建文件夹  
             String fileName = "/sdcard/myImage/"+name; 
             //editText.setText(fileName);
             try {  
                 FOut = new FileOutputStream(fileName);  
                 ori_rbitmap.compress(Bitmap.CompressFormat.JPEG, 100, FOut);// 把数据写入文件  
             } catch (FileNotFoundException e) {  
                 e.printStackTrace();  
             }finally {  
                 try {  
                     FOut.flush();  
                     FOut.close();  
                 } catch (IOException e) {  
                     e.printStackTrace();  
                 }  
             }
             String myPath=fileName;//接着根据存放的路径获取图片放到EditText中
             Log.w(myPath,"fileName");
             Toast.makeText(this, myPath, Toast.LENGTH_SHORT).show();
             SpannableString span_str = new SpannableString(myPath);
             Bitmap my_bm=BitmapFactory.decodeFile(myPath);
             Bitmap my_rbm=resizeimg.resizeImage(my_bm, 300, 300);
             ImageSpan span = new ImageSpan(this, my_rbm);
             span_str.setSpan(span, 0, myPath.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
             Editable et = editText.getText();// 先获取Edittext中的内容
             int start = editText.getSelectionStart();
             et.insert(start, span_str);// 设置ss要添加的位置
             editText.setText((CharSequence)et);// 把et添加到Edittext中
             editText.setSelection(start + span_str.length());// 设置Edittext中光标在最后面显示

        }
        if(requestCode==0x222&&resultCode==RESULT_OK){//requestCode=0x222是拍照
            String sdStatus = Environment.getExternalStorageState();  
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
                Log.i("TestFile","SD card is not avaiable/writeable right now.");                   
             } 

             String name =Calendar.getInstance(Locale.CHINA).getTimeInMillis() + ".jpg";//给拍的照片命名,下面进行存储
             Bundle bundle = data.getExtras();  
             Bitmap camera_bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
             Bitmap camera_rbitmap=resizeimg.resizeImage(camera_bitmap, 300, 300);
             FileOutputStream FOut = null;    
             File file = new File("/sdcard/myImage/");  
             file.mkdirs();// 创建文件夹  
             String fileName = "/sdcard/myImage/"+name; 
             try {  
                 FOut = new FileOutputStream(fileName);  
                 camera_rbitmap.compress(Bitmap.CompressFormat.JPEG, 100, FOut);// 把数据写入文件  
             } catch (FileNotFoundException e) {  
                 e.printStackTrace();  
             }finally {  
                 try {  
                     FOut.flush();  
                     FOut.close();  
                 } catch (IOException e) {  
                     e.printStackTrace();  
                 }  
             }

             String myPath=fileName;//下面从文件夹中取出来放到EditText中去
             SpannableString span_str=new SpannableString(myPath);
             Bitmap my_bitmap=BitmapFactory.decodeFile(myPath);
             Bitmap my_rbitmap=resizeimg.resizeImage(my_bitmap, 300, 300);
             ImageSpan span=new ImageSpan(this,my_rbitmap);
             span_str.setSpan(span,0,myPath.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
             Editable et = editText.getText();// 先获取Edittext中的内容
             int start = editText.getSelectionStart();
             et.insert(start, span_str);// 设置ss要添加的位置
             editText.setText((CharSequence)et);// 把et添加到Edittext中
             editText.setSelection(start + span_str.length());// 设置Edittext中光标在最后面显示


        }
        //+_+_+_+_+获取图片+_+_+_+_+//

最后从数据库提取时需要调用一个解析的方法:

//+_+_+_+_+_+_+_+_+_+_+_+_+定义一个解析图片的方法+_+_+_+_+_+_+_+_
    public CharSequence analyzeImage(String content){
        String my_content=content;
        SpannableString span_str=new SpannableString(content);
        Pattern p=Pattern.compile("chemming/sdcard/myImage/[0-9]{13}+.jpg");
        Matcher m=p.matcher(content);
        while(m.find()){
            String mypath=m.group();
            Toast.makeText(this, m.group(), Toast.LENGTH_SHORT);
            Bitmap bitmap=BitmapFactory.decodeFile(mypath);
            Bitmap rbitmap=resizeimg.resizeImage(bitmap, 300, 300);
            ImageSpan span=new ImageSpan(this,rbitmap);
            span_str.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return span_str;
    }
    //+_+_+_+_+_+_+_+_+_+_+_+_+定义一个解析图片的方法+_+_+_+_+_+_+_+_//

你可能感兴趣的:(Android,EditText,图文混存,图片解析)