【问题标题】:How can I put the Gallery image into the Edit Text Randomly using drawable如何使用drawable将图库图像随机放入编辑文本中
【发布时间】:2014-06-05 06:19:52
【问题描述】:

在我的应用程序中,我尝试将图库图像随机放入编辑文本中,我需要像聊天应用程序一样,我们可以在编辑文本中键入任何文本,我们还需要在用户需要的任何地方添加图库图像和表情符号可以选择并放入编辑文本有什么想法。

【问题讨论】:

标签: android android-imageview android-drawable android-gallery


【解决方案1】:

根据this

你应该使用 Spannable 字符串并解析你的表情!!!

【讨论】:

【解决方案2】:

请参考这个问题:StackOverFlow Question

它使用这样的东西:

b = (Button) findViewById(R.id.Button01))
        b.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                // in onCreate or any event where your want the user to
                // select a file
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Select Picture"), SELECT_PICTURE);
            }
        });

【讨论】:

  • 我也试过这个代码,但编辑文本图像会出现在右侧角落请指导我
  • 此代码不能用于将图像放入编辑文本中,请给我有用的答案。
  • 图像到 EditText?这就像在数学考试中写苯的合成。
【解决方案3】:

试试这个

int position = Selection.getSelectionStart(AgendaWriterEditText.this.getText());

Spanned e = Html.fromHtml("<img src=\"" + imageResource + "\">", imageGetter, null);
// String s2 = Html.toHtml(e);
// Log.w("Agenda", "Before DB->Image is: " + s2);

AgendaWriterEditText.this.getText().insert(position, e);

感谢大家的回复

我已经通过从图库中获取图像并使用上面的代码插入到编辑文本中解决了这个问题,获取drawable的方法是

// setting image
    holder.dwEdit.setImageGetter(new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            Drawable drawable = null;

            try {

                // declaring views
                ImageView topicImage = new ImageView(_context);
                // setting data
                topicImage.setImageURI(Uri.parse(source));

                // getting Drawable from image to use in HTML <img tag
                drawable = topicImage.getDrawable();

                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

                drawable = new BitmapDrawable(_context.getResources(),
                        bitmap);

                // Important
                if (drawable != null) {
                    // setting the bounds (width + height)
                    drawable.setBounds(0, 0, 200, 200);
                }
            } catch (Exception e) {
                Log.e("Hammad", "Failed to load inline image!");
            }
            return drawable;
        }
    });

重要

当编辑文本读取标签时调用此方法,并将 src="path" 给此方法,该代码将返回 Drawable

终于可以添加图片来编辑文本字段了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2021-01-18
    • 2013-03-10
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多