I am on a Notetaking project where I want to allow users to insert images in their notes. the image will be on top or below of texts. this is the code by which I am trying to get the image into the editText field. But after selecting the image it does not show in the edit Text. it will be very helpful if anybody can help me. this is in my 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);
this is my 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 will show in image view
, if you want to upload image from gallery then this code will work
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());
}
}
}
Layout will be like this
<?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>