Android基础开发-选择图片,发送彩信

发送图片彩信案例:

按住加号,选择相册,把相册选择的图片加载的应用中,点击发送彩信,选择短信,发送彩信。

代码如下:

java 复制代码
package com.example.client;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class SendMMSActivity extends AppCompatActivity {

    ActivityResultLauncher<Intent> mResultLauncher;

    ImageView iv_append;
    Uri selectedImageUri;

    private static final int PICK_IMAGE_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_m_m_s);
        iv_append = findViewById(R.id.iv_append);
    }


    /**
     * 跳转到系统的相册,选择图片,并返回
     *
     * @param view
     */
    public void selectPic(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            selectedImageUri = data.getData();
            iv_append.setImageURI(selectedImageUri);
        }
    }

    public void sendMms(View view) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setType("image/*");
        //彩信发送的目标号码
        intent.putExtra("address", 10086);
        intent.putExtra("subject", "测试发送彩信");
        intent.putExtra("sms_body", "测试发送彩信一下");
        intent.putExtra(Intent.EXTRA_STREAM, selectedImageUri);
        startActivity(intent);
        Toast.makeText(this, "请在弹窗中选择短信应用", Toast.LENGTH_SHORT).show();
    }
}

结果:

相关推荐
撩得Android一次心动30 分钟前
Android中Gradle网络下载超时、更改存放位置(解决方案)
android·gradle
HQL_seven2 小时前
【Activity】Android Activity详解
android
AI2中文网2 小时前
安卓 15/16 手机软键盘遮挡输入框、布局顶不上去?已解决:重新编译即可
android·智能手机
字节暗面2 小时前
Android APP 加固横评记录:爱加密、梆梆、网易易盾与腾讯 ACE
android·逆向工程·加固
落魄Android在线炒饭3 小时前
Android 实战问题反推 OpenGL 概念(上)
android·opengl
booboosui3 小时前
DeepSeek 的 Worker 调得动 GitHub,调不动微信:三类 Worker 的实现差别
android
MiyamuraMiyako4 小时前
Compose LazyRow 实现尾项露出与少量数据均分
android·android jetpack
booboosui4 小时前
输入法借 350 毫秒:给 agent 打字的两条路
android
哲鲤7 小时前
Binder 不只是 IPC:从一次跨进程调用看懂 Android 与 AAOS 的架构演进
android
用户128139778167 小时前
Eyf半字节【3.0 版】正式发布|订单、单证、扫码三大核心模块全新重构
android