qq_25824541 2017-10-28 14:57 采纳率: 80%
浏览 1381
已采纳

记事本开发在edittext中插入图片的问题

NoteEdit.java

public class NoteEdit extends AppCompatActivity {
private EditText edit_content;
private Database DBHelper;
public int enter_state = 0;//用来区分是新建一个note还是更改原来的note
public String last_content;//用来获取edittext内容

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editnote);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_edit);
    setSupportActionBar(toolbar);
    InitView();

}

private Bitmap resizeImage(Bitmap bitmap, int wid, int hei) {
    Bitmap BitmapOrg = bitmap;
    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = wid;
    int newHeight = hei;

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizeBipmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    return resizeBipmap;
}

private void InitView() {
    edit_content = (EditText) findViewById(R.id.edit_content);
    //fab_ok = (FloatingActionButton) findViewById(R.id.fab_ok);
    DBHelper = new Database(this);

    //获取此时时刻时间
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String dateString = sdf.format(date);

    //接收内容和id
    Bundle myBundle = this.getIntent().getExtras();
    last_content = myBundle.getString("info");
    enter_state = myBundle.getInt("enter_state");
    edit_content.setText(last_content);
}


@Override
protected void onPause() {
    super.onPause();
    SQLiteDatabase db = DBHelper.getReadableDatabase();
    String content = edit_content.getText().toString();
    if (enter_state == 0) {
        if (!content.equals("")) {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String dateString = sdf.format(date);
            Bundle data = new Bundle();
            data.putString("nei",content);
            Intent intent = new Intent(NoteEdit.this,Display.class);
            intent.putExtra("neirong",data);
            ContentValues values = new ContentValues();
            values.put("content", content);
            values.put("date", dateString);
            db.insert("note", null, values);
            finish();
        }
    } else {
        ContentValues values = new ContentValues();
        values.put("content", content);
        db.update("note", values, "content=?", new String[]{last_content});
        finish();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_paint:
            Intent getImage = new Intent(Intent.ACTION_PICK, null);
            getImage.addCategory(Intent.CATEGORY_OPENABLE);
            getImage.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            getImage.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(getImage, 1);
            break;
    }
    return true;
}

protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, intent);
    ContentResolver resolver = getContentResolver();
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Uri originalUri = intent.getData();
            try {
                Bitmap originalBitmap = BitmapFactory.decodeStream(resolver
                        .openInputStream(originalUri));
                Bitmap bitmap = resizeImage(originalBitmap, 100, 100);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            edit_content = (EditText) findViewById(R.id.edit_content);
            insertIntoEditText(getBitmapMime(bitmap, originalUri));
        }
    }
}

private SpannableString getBitmapMime(int pic, Uri uri) {
    String path = uri.getPath();
    SpannableString ss = new SpannableString(path);
    ImageSpan span = new ImageSpan(this, pic);
    ss.setSpan(span, 0, path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ss;
}

private void insertIntoEditText(SpannableString ss) {
    Editable et = edit_content.getText();// 先获取Edittext中的内容
    int start = edit_content.getSelectionStart();
    et.insert(start, ss);// 设置ss要添加的位置
    edit_content.setText(et);// 把et添加到Edittext中
    edit_content.setSelection(start + ss.length());// 设置Edittext中光标在最后面显示
}

@Override
public void onStart() {
    super.onStart();
}

@Override
public void onStop() {
    super.onStop();
}

}

Display.java

public class Display extends AppCompatActivity{
EditText display;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);
    display = (EditText) findViewById(R.id.display);
    String imagePath = getIntent().getBundleExtra("neirong").getString(
            "nei");
    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 = resizeImage(bm, 100, 100);
        ImageSpan span = new ImageSpan(this, rbm);
        ss.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    display.setText(ss);
}

private Bitmap resizeImage(Bitmap bitmap, int wid, int hei) {
    Bitmap BitmapOrg = bitmap;
    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = wid;
    int newHeight = hei;

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizeBipmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    return resizeBipmap;
}

}
为什么我在记事界面点击插入图片后程序崩溃了
错误日志:java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:37152 flg=0x1 }} to activity {com.example.mingnote/com.example.mingnote.NoteEdit}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()' on a null object reference

  • 写回答

2条回答 默认 最新

  • 紫风幻雪 2017-10-30 00:50
    关注

    你这错误是空指针,从你的Log上来看,是你的Drawable的使用上出现了空指针

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!