android studio에서 인텐트를 통해 여러 콘텐츠를 보내는 방법 (how can i send multiple contents through intent in android studio)


문제 설명

android studio에서 인텐트를 통해 여러 콘텐츠를 보내는 방법 (how can i send multiple contents through intent in android studio)

인텐트를 통해 텍스트와 이미지를 모두 보내고 싶지만 아무 것도 전송되지 않습니다. 내가 사용한 코드가 여기에 언급되어 있습니다. 인텐트를 통해 입력 및 클릭한 이미지의 세부 정보를 보내려고 합니다. 어디가 잘못되었는지 알려주십시오. 클릭한 이미지를 보내는 다른 방법이 있습니까?

MAINACTIVITY.JAVA:

public class MainActivity extends AppCompatActivity {
CheckBox cb1, cb2;
Button export, button;
//ImageView img;
Uri file;
EditText name, age, address, phone;
String genderr, namee, agee, addr, num;
private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    export = findViewById(R.id.button);
    cb1 = findViewById(R.id.male);
    cb2 = findViewById(R.id.female);
    imageView = (ImageView) this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
    export.setOnClickListener(new View.OnClickListener()
    {
            @Override
            public void onClick(View view) {
                name = findViewById(R.id.et_name);
                name.setTypeface(null, Typeface.BOLD);
                age = findViewById(R.id.et_age);
                age.setTypeface(null, Typeface.BOLD);
                address = findViewById(R.id.address);
                phone = findViewById(R.id.phone);
                if (cb1.isChecked()) {
                    cb2.setChecked(false);
                    genderr = cb1.getText().toString();
                } else if (cb2.isChecked()) {
                    cb1.setChecked(false);
                    genderr = cb2.getText().toString();
                }

                final String content = "NAME: " + name.getText().toString() + "\n AGE: " + age.getText().toString() + "\n Gender: " + genderr + "\n ADDRESS: " + address.getText().toString() + "\n Phone: " + phone.getText().toString()+"\n IMAGE:";
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra(Intent.EXTRA_TEXT, content);
            sendIntent.putExtra(Intent.EXTRA_STREAM, (Parcelable) imageView);
              //  Intent sendIntent = new Intent(Intent.ACTION_PICK);
                sendIntent.setType("image/*");
              //  startActivityForResult(in, imageView);
                //  sendIntent.putExtra(Intent.EXTRA_TEXT, extraParams);
                startActivity(Intent.createChooser(sendIntent, "Send Using.."));
            }
        });
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);

ACTIVITY_MAIN:

  <ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/name"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView2"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#72C8E8"
            android:text="@string/age"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView3"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/gender"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <CheckBox
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:onClick="onCheckboxClicked"
            android:text="@string/m"
            android:textSize="24sp" />

        <CheckBox
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:onClick="onCheckboxClicked"
            android:text="@string/f"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/address"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPostalAddress" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_marginTop="20dp"
            android:layout_height="wrap_content"
            android:text="@string/phone"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/camera"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold"/>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="281dp"
            android:layout_height="154dp"
            android:layout_above="@+id/button1"
            android:layout_alignParentTop="false"
            android:layout_gravity="center_horizontal"
            app:srcCompat="@drawable/ic_launcher_background" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="24dp"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/img"
            android:textSize="24sp" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="24dp"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/export"
            android:textSize="24sp" />

    </LinearLayout>
</ScrollView>

MANIFEST:

<uses‑permission android:name="android.permission.INTERNET"/>
<uses‑permission android:name="android.permission.CAMERA"/>
<uses‑permission‑sdk‑23 android:name="android.permission.CAMERA"/>
<uses‑permission‑sdk‑23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses‑permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses‑permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses‑permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses‑permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses‑feature
    android:name="android.hardware.camera"
    android:required="false"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent‑filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent‑filter>
    </activity>
</application>


참조 솔루션

방법 1:

In onActivityResult() check the request code and store bitmap to get its URI

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
        Uri imageUri = getImageUri(getApplicationContext(), photo);
        shareImageText(imageUri);
    }  
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

share image and text via intent like

private vois shareImageText(Uri uri)  {
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra(Intent.EXTRA_TEXT,"Dummy Text");
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent,"Share"));
}

Hope it will help.

(by user9919669Ravi)

참조 문서

  1. how can i send multiple contents through intent in android studio (CC BY‑SA 2.5/3.0/4.0)

#android-intent #image #Android #text






관련 질문

인텐트를 사용하여 메시지 작성 열기 (Opening create message using an intent)

다른 데이터로 일부 인텐트를 생성하더라도 알림에서 동일한 추가 데이터를 얻습니다. (I get the same extra data from notifications even if I create some intents with different data)

내 앱의 피드백 양식에 채워진 텍스트를 내 이메일 주소로 가져오는 방법은 무엇입니까? 자세한 내용을 참조하십시오 (How to get the text filled in feedback form of my app to my email address? Please see details)

Android 이미지 갤러리 이미지 자르기 및 ImageView 설정 (Android Image Gallery Crop Image and set ImageView)

xamarin에서 Android 활동 간에 데이터를 전달할 수 없습니다. (Can't pass data between android activities in xamarin)

모든 애플리케이션에 대한 Android 앱에서 데이터 공유 (Share Data in android app for all applications)

zxing이 브라우저에서 URL을 열도록 하거나 스캔 후 번호를 다이얼하는 방법 (How to make zxing open a URL in a browser or dial a number after scanning)

Android 암시적 의도 유효성 검사 (Android implicit intents validation)

kaldi 설치 시 libmkl_tbb_thread.so sth 관련 문제 (A problem related to libmkl_tbb_thread.so sth when installing kaldi)

Intent를 통해 데이터 전달 및 수신 (Passing data through Intent and receiving it)

android studio에서 인텐트를 통해 여러 콘텐츠를 보내는 방법 (how can i send multiple contents through intent in android studio)

Kotlin의 다른 EditText 값에 대한 의도 EditText 값 (Intent EditText values to another EditText values in Kotlin)







코멘트