如何将图像从画廊插入到 EditTex 视图?
Posted
技术标签:
【中文标题】如何将图像从画廊插入到 EditTex 视图?【英文标题】:how to insert Image from gallery to a EditTex View? 【发布时间】:2022-01-21 17:59:21 【问题描述】:我正在进行一个笔记项目,我希望允许用户在他们的笔记中插入图像。图像将位于文本的顶部或下方。这是我试图将图像放入editText字段的代码。但是在选择图像后,它不会显示在编辑文本中。如果有人可以帮助我,那将非常有帮助。 这是在我的 onCreate 中:
img = findViewById(R.id.addImage);
LinearLayout layoutCustomization = findViewById(R.id.miscellaneous_layout);
initialCustomizationOption(layoutCustomization);
mGetImg = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>()
@Override
public void onActivityResult(Uri result)
uriImg = result;
try
bitmap = MediaStore.Images.Media.getBitmap(InsertActivity.this.getContentResolver(), uriImg);
catch (IOException e)
e.printStackTrace();
);
ImageSpan imageSpan = new ImageSpan(InsertActivity.this,bitmap,0);
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(binding.noteData.getText());
String imgId = "[img=1]";
int selStart = binding.noteData.getSelectionStart();
builder.replace(binding.noteData.getSelectionStart(), binding.noteData.getSelectionEnd(), imgId);
builder.setSpan(imageSpan, selStart, selStart + imgId.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
binding.noteData.setText(builder);
这是我的 XML:
<EditText
android:id="@+id/noteData"
android:layout_
android:layout_
android:layout_margin="16dp"
android:layout_marginBottom="50dp"
android:autoSizeTextType="uniform"
android:background="@drawable/edit_text_bg"
android:fontFamily="@font/product_sans_regular"
android:gravity="start"
android:hint="Notes..."
android:overScrollMode="always"
android:padding="20dp"
android:scrollbarStyle="insideInset"
android:textSize="18sp" />
【问题讨论】:
图像应该被插入到ImageView 小部件,而不是用于捕获用户文本输入的Edittext。 @Tonnie 感谢您的回答。有什么办法可以在文本上方或下方插入图像?如果我在那里放置一个图像视图,那么之前的图像将被替换而不是添加一个新图像。 单个ImageView
一次只呈现一张图片。您可能需要ListView
来展示多张图片。请看一下这个解决方案:***.com/a/54992231/2828685
@A.K.MMUHIBULLAHNAYEM 如果我的回答有效,请标记为答案。如果您认为我真的付出了一些努力,谢谢:)
【参考方案1】:
图片将显示在image view
中,如果您想从图库上传图片,则此代码将起作用
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.IOException;
public class TestImage extends AppCompatActivity
ImageView img;
TextView textView;
private static final int PICK_IMAGE_REQUEST = 1;
private Uri filePath;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_image);
img = findViewById(R.id.addImage);
textView = findViewById(R.id.textview);
img.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (getApplicationContext() != null && getApplicationContext().getPackageName().equals(BuildConfig.APPLICATION_ID))
startActivityForResult(intent, PICK_IMAGE_REQUEST);
);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
try
switch (requestCode)
case PICK_IMAGE_REQUEST:
if (resultCode == Activity.RESULT_OK)
//data gives you the image uri. Try to convert that to bitmap
filePath = data.getData();
try
bitmap = MediaStore.Images.Media.getBitmap(TestImage.this.getContentResolver(), filePath);
img.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
break;
else if (resultCode == Activity.RESULT_CANCELED)
Log.e("TestImage", "Selecting picture cancelled");
break;
catch (Exception e)
Log.e("TestImage", "Exception in onActivityResult : " + e.getMessage());
布局会是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".TestImage"
android:orientation="vertical">
<ImageView
android:layout_
android:layout_
android:src="@mipmap/ic_launcher"
android:id="@+id/addImage"/>
<TextView
android:layout_
android:layout_
android:gravity="center"
android:text="Here is your text"
android:textSize="50sp"
android:id="@+id/textview"/>
</LinearLayout>
【讨论】:
非常感谢您的帮助。以上是关于如何将图像从画廊插入到 EditTex 视图?的主要内容,如果未能解决你的问题,请参考以下文章