簡體   English   中英

如何將圖像從畫廊插入到 EditTex 視圖?

[英]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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM