如何将图库中的图片插入到 EditText 视图中?
how to insert Image from gallery to a EditTex View?
我正在做一个笔记项目,我想让用户在他们的笔记中插入图像。图像将位于文本的顶部或下方。这是我试图将图像放入 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_width="match_parent"
android:layout_height="wrap_content"
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" />
图片将显示在 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_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestImage"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="250sp"
android:src="@mipmap/ic_launcher"
android:id="@+id/addImage"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Here is your text"
android:textSize="50sp"
android:id="@+id/textview"/>
</LinearLayout>
我正在做一个笔记项目,我想让用户在他们的笔记中插入图像。图像将位于文本的顶部或下方。这是我试图将图像放入 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_width="match_parent"
android:layout_height="wrap_content"
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" />
图片将显示在 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_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestImage"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="250sp"
android:src="@mipmap/ic_launcher"
android:id="@+id/addImage"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Here is your text"
android:textSize="50sp"
android:id="@+id/textview"/>
</LinearLayout>