效果:
点击:发送彩信
在发送信息页面:
逻辑处理
SendMmsActivity.java
import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import com.kcs.contentshare_client.util.ToastUtil; public class SendMmsActivity extends AppCompatActivity implements View.OnClickListener { private ImageView iv_appendix; private ActivityResultLauncher<Intent> mResultLauncher; private EditText et_phone; private EditText et_title; private EditText et_message; private Uri picUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_mms); et_phone = findViewById(R.id.et_phone); et_title = findViewById(R.id.et_title); et_message = findViewById(R.id.et_message); iv_appendix = findViewById(R.id.iv_appendix); iv_appendix.setOnClickListener(this); findViewById(R.id.btn_send_mms).setOnClickListener(this); // 跳转到系统相册,选择图片,并返回 mResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if (result.getResultCode() == RESULT_OK) { Intent intent = result.getData(); // 获得选中图片的路径对象 // 存放图片的路径/storage/emulated/0/Download picUri = intent.getData(); //选择图片返回后,就显示图片 if (picUri != null) { // ImageView 显示刚刚选中的图片 iv_appendix.setImageURI(picUri); Log.d("十二", "picUri:" + picUri.toString()); } } } }); } @Override public void onClick(View v) { switch (v.getId()) { //添加图片 case R.id.iv_appendix: // 跳转到系统相册,选择图片,并返回 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // 设置内容类型为图片类型 intent.setType("image/*"); // 打开系统相册,并等待图片选择结果 mResultLauncher.launch(intent); break; case R.id.btn_send_mms: // 发送带图片的彩信 sendMms(et_phone.getText().toString(), et_title.getText().toString(), et_message.getText().toString()); break; default: break; } } /** *发送带图片的彩信 */ private void sendMms(String phone, String title, String message) { Intent intent = new Intent(Intent.ACTION_SEND); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Intent 的接受者将被准许读取Intent 携带的URI数据 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 彩信发送的目标号码 intent.putExtra("address", phone); // 彩信的标题 intent.putExtra("subject", title); // 彩信的内容 intent.putExtra("sms_body", message); // 彩信的图片附件 intent.putExtra(Intent.EXTRA_STREAM, picUri); // 彩信的附件为图片 intent.setType("image/*"); // 因为未指定要打开哪个页面,所以系统会在底部弹出选择窗口 startActivity(intent); ToastUtil.show(this, "请在弹窗中选择短信或者信息应用"); } }
工具类
import android.content.Context; import android.widget.Toast; public class ToastUtil { public static void show(Context ctx, String desc) { Toast.makeText(ctx, desc, Toast.LENGTH_SHORT).show(); } }
布局
activity_send_mms.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="对方号码:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_phone" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:inputType="number" android:text="10086" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="彩信标题:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_title" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:text="hallo" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="彩信内容:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_message" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:gravity="left|top" android:text="test" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="top|center" android:text="图片附件:" android:textColor="@color/black" android:textSize="17sp" /> <ImageView android:id="@+id/iv_appendix" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:scaleType="fitStart" android:src="@drawable/add_potoes_32" /> </LinearLayout> <Button android:id="@+id/btn_send_mms" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="发送彩信" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout>