在我的应用程序中,我将从图库中选择图像并将其放入编辑文本,用户可以将图像放在编辑文本字段中,如果有任何想法,用户也可以在编辑文本字段中键入任何内容救命。
Emoticon.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emoticon);
Pick = (Button)findViewById(R.id.pick);
EditText text = (EditText) findViewById(R.id.editText);
Pick.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent();
// Picture from camera
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 10);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == requestCode) {
// return from file upload
if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
SpannableString ss = new SpannableString("abc");
Drawable d = img.getDrawable();
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
EditText.setText(ss);
}
}
}
答案 0 :(得分:0)
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String filePath) {
Drawable d = Drawable.createFromPath(filePath);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
cs = Html.fromHtml(
"<img src='"
+ filePath
+ "'/>", imageGetter, null);
edttxtemoji.setText(cs);
你可以试试上面的代码。 filePath是用户选择的文件的路径。