如何在Android系統中透過彩信傳送圖片?


介紹

多媒體資訊服務 (MMS) 是一種透過手機向不同聯絡人傳送圖片和其他媒體檔案以及文字的好方法。如果您想將圖片傳送給您的聯絡人,那麼 MMS 是傳送給朋友和家人的好方法。在本文中,我們將瞭解如何在 Android 系統中透過 MMS 傳送圖片。

實現

我們將建立一個簡單的應用程式,其中我們將建立一個文字檢視來顯示應用程式的標題。之後,我們將建立一個影像檢視來顯示從相簿中選擇的圖片。之後,我們將顯示一個按鈕,用於從相簿中選擇要傳送的圖片。然後,我們將建立一個編輯文字,用於新增 MMS 的簡訊正文。之後,我們將顯示一個按鈕,我們將使用它來發送 MMS。現在讓我們轉向 Android Studio 來建立一個新的 Android Studio 專案。

步驟 1:在 Android Studio 中建立一個新專案

導航到 Android Studio,如下面的螢幕所示。在下面的螢幕中,單擊“新建專案”以建立一個新的 Android Studio 專案。

單擊“新建專案”後,您將看到下面的螢幕。

在這個螢幕中,我們只需選擇“空活動”,然後單擊“下一步”。單擊“下一步”後,您將看到下面的螢幕。

在這個螢幕中,我們只需指定專案名稱。然後包名將自動生成。

注意:確保選擇 Java 作為語言。

指定所有詳細資訊後,單擊“完成”以建立一個新的 Android Studio 專案。

專案建立完成後,我們將看到開啟的兩個檔案:activity_main.xml 和 MainActivity.java 檔案。

步驟 2:使用 activity_main.xml

導航到 activity_main.xml。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>res>layout>activity_main.xml 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋以詳細瞭解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

   <!-- on below line creating the heading of our application -->
   <TextView
       android:id="@+id/idTVHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_above="@id/idIVImage"
       android:layout_margin="10dp"
       android:layout_marginTop="90dp"
       android:padding="8dp"
       android:text="Send image via MMS in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />

   <!-- on below line creating image view for displaying the image picked from gallery -->
   <ImageView
       android:id="@+id/idIVImage"
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:layout_above="@id/idBtnChooseImage"
       android:layout_centerHorizontal="true"
       android:layout_margin="10dp" />

   <!-- on below line creating a button to pick image from internal storage -->
   <Button
       android:id="@+id/idBtnChooseImage"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:layout_margin="20dp"
       android:text="Choose Image"
       android:textAllCaps="false" />

   <!--on below line creating edit text for entering sms body -->
   <EditText
       android:id="@+id/idEdtBody"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idBtnChooseImage"
       android:layout_margin="10dp"
       android:hint="Enter SMS Body" />

   <!-- on below line creating a button to send sms -->
   <Button
       android:id="@+id/idBtnSendMMS"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idEdtBody"
       android:layout_margin="20dp"
       android:text="Send MMS"
       android:textAllCaps="false" />
</RelativeLayout>

說明:在上面的程式碼中,我們建立了一個根佈局作為相對佈局。在這個佈局中,我們建立一個文字檢視,用於顯示應用程式的標題。之後,我們建立一個影像檢視,用於顯示使用者選擇的圖片。在這個影像檢視之後,我們顯示一個用於選擇該圖片的按鈕。然後,我們顯示一個編輯文字,用於輸入簡訊正文。之後,我們顯示另一個按鈕,用於透過手機中的預設簡訊應用程式傳送 MMS 訊息。

步驟 3:在 AndroidManifest.xml 檔案中新增許可權

導航到 app>AndroidManifest.xml 檔案,並在 manifest 標籤中新增以下許可權,以讀取外部儲存中的圖片併發送簡訊。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>

步驟 4:使用 MainActivity.java 檔案

導航到 MainActivity.java。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>res>layout>MainActivity.java 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋以詳細瞭解。

package com.example.java_test_application;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
   // creating variable for button on below line.
   private Button chooseImgBtn, sendMMSBtn;
   // creating variable for image view on below line.
   private ImageView imageView;
   // creating variable for image uri on below line.
   private String imageUri = "";
   // creating variable for edit text on below line.
   private EditText msgEdt;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // initializing variables for button on below line.
       chooseImgBtn = findViewById(R.id.idBtnChooseImage);
       sendMMSBtn = findViewById(R.id.idBtnSendMMS);
       // initializing variable for image view on below line.
       imageView = findViewById(R.id.idIVImage);
       // initializing variable for edit text on below line.
       msgEdt = findViewById(R.id.idEdtBody);
       // on below line adding click listner for choose image button.
       chooseImgBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line choosing image using intent.
               Intent i = new Intent();
               // on below line setting intent type.
               i.setType("image/*");
               // on below line setting action for intent.
               i.setAction(Intent.ACTION_GET_CONTENT);
               // on below line starting activity for result to select the image..
               startActivityForResult(Intent.createChooser(i, "Select Picture"), 100);
           }
       });
       // on below line adding click listener for send mms button.
       sendMMSBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line checking if the edit text field is empty.
               if (msgEdt.getText().toString().isEmpty()) {
                   // if edit text field is empty displaying a toast message.
                   Toast.makeText(MainActivity.this, "Please enter MMS Body", Toast.LENGTH_SHORT).show();
               } else {
                   // on below line creating an intent to send sms
                   Intent sendIntent = new Intent(Intent.ACTION_SEND);
                   // on below line putting extra as sms body with the data from edit text
                   sendIntent.putExtra("sms_body", msgEdt.getText().toString());
                   // on below line putting extra as image uri
                   sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUri));
                   // on below line setting intent type.
                   sendIntent.setType("image/png");
                   // on below line starting activity to send sms.
                   startActivity(sendIntent);
               }
           }
       });
   }
   // calling on activity result method.
   @Override
   protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       // on below line checking if result is ok.
       if (resultCode == RESULT_OK) {
           // on below line checking for request code.
           if (requestCode == 100) {
               // on below line getting the selected image uri.
               Uri selectedImageUri = data.getData();
               // on below line checking if selected image uri is not null.
               if (null != selectedImageUri) {
                   // setting image uri in our variable.
                   imageUri = selectedImageUri.toString();
                   // loading the image from image uri in the image view
                   imageView.setImageURI(selectedImageUri);
               }
           }
       }
   }
}

說明:在上面的程式碼中,我們首先為按鈕、編輯文字、影像檢視和一個用於儲存影像 URI 的字串建立變數。現在我們將看到 onCreate 方法。這是每個 Android 應用程式的預設方法。當建立應用程式檢視時,將呼叫此方法。在此方法中,我們將設定內容檢視,即名為 activity_main.xml 的佈局檔案,以設定該檔案中的 UI。在 onCreate 方法中,我們使用我們在 activity_main.xml 檔案中指定的 ID 初始化不同的變數,例如文字按鈕、影像檢視和編輯文字。

之後,我們為“選擇圖片”按鈕新增一個點選監聽器。在此方法中,我們呼叫一個意圖,該意圖用於透過呼叫 startActivityForResult 方法從裝置儲存中選擇圖片。現在,在 activityResult 方法中,我們獲取影像 URI,並在我們的影像檢視中設定來自該影像 URI 的影像。之後,我們為“傳送 MMS”按鈕新增一個點選監聽器。在此按鈕的點選監聽器中,我們首先驗證簡訊正文文字是否為空,然後呼叫意圖來發送包含附加圖片的 MMS 訊息。

新增上述程式碼後,我們只需單擊頂部的綠色圖示即可在移動裝置上執行我們的應用程式。

注意:確保已連線到您的真實裝置或模擬器。

輸出

注意:由於裝置是模擬器,因此我們無法傳送 MMS 訊息,但我們可以在真實裝置上傳送相同的訊息。

結論

在本文中,我們瞭解瞭如何在 Android 系統中透過 MMS 傳送圖片。

更新於:2023年5月9日

1K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.