이 예는 Kotlin을 사용하여 Android에서 파일이 첨부된 이메일을 보내는 방법을 보여줍니다.
1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="4dp" tools:context=".MainActivity"> <EditText android:id="@+id/etTo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:hint="Receiver's Email Address!" android:inputType="textEmailAddress" android:singleLine="true" /> <EditText android:id="@+id/etSubject" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:hint="Enter Subject" android:singleLine="true" /> <EditText android:id="@+id/etMessage" android:layout_width="match_parent" android:layout_height="200dp" android:layout_margin="5dp" android:gravity="top|start" android:hint="Compose Email" android:inputType="textMultiLine" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btSend" android:layout_width="80dp" android:layout_height="50dp" android:layout_margin="5dp" android:text="Send" /> <Button android:id="@+id/btAttachment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:text="attachment" /> </RelativeLayout> <TextView android:id="@+id/tvAttachment" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableStart="@drawable/ic_baseline_attach_file_24" android:visibility="gone" /> </LinearLayout>
3단계 − src/MainActivity.kt에 다음 코드 추가
import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.View import android.widget.Button import android.widget.EditText import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { lateinit var etEmail: EditText lateinit var etSubject: EditText lateinit var etMessage: EditText lateinit var send: Button lateinit var attachment: Button lateinit var tvAttachment: TextView lateinit var email: String lateinit var subject: String lateinit var message: String lateinit var uri: Uri private val pickFromGallery:Int = 101 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" etEmail = findViewById(R.id.etTo) etSubject = findViewById(R.id.etSubject) etMessage = findViewById(R.id.etMessage) attachment = findViewById(R.id.btAttachment) tvAttachment = findViewById(R.id.tvAttachment) send = findViewById(R.id.btSend) send.setOnClickListener { sendEmail() } attachment.setOnClickListener { openFolder() } } private fun openFolder() { val intent = Intent() intent.type = "image/*" intent.action = Intent.ACTION_GET_CONTENT intent.putExtra("return-data", true) startActivityForResult(Intent.createChooser(intent, "Complete action using"), pickFromGallery) } private fun sendEmail() { try { email = etEmail.text.toString() subject = etSubject.text.toString() message = etMessage.text.toString() val emailIntent = Intent(Intent.ACTION_SEND) emailIntent.type = "plain/text" emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email)) emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject) emailIntent.putExtra(Intent.EXTRA_STREAM, uri) emailIntent.putExtra(Intent.EXTRA_TEXT, message) this.startActivity(Intent.createChooser(emailIntent, "Sending email...")) } catch (t: Throwable) { Toast.makeText(this, "Request failed try again: $t", Toast.LENGTH_LONG).show() } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == pickFromGallery && resultCode == RESULT_OK) { if (data != null) { uri = data.data!! } tvAttachment.text = uri.lastPathSegment tvAttachment.visibility = View.VISIBLE } } }
4단계 − androidManifest.xml에 다음 코드 추가
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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> </manifest>
응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 실행을 클릭합니다. 툴바에서 아이콘. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오.