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();
    }
}

结果:

相关推荐
summerkissyou198713 分钟前
Android 16 架构图
android
神龙天舞200140 分钟前
MySQL 备库为什么会延迟好几个小时
android·数据库·mysql
码农coding4 小时前
android12 systemUI 之锁屏
android
圆山猫4 小时前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v
爱笑鱼4 小时前
Binder(七):getCallingUid() 读到的是谁?clearCallingIdentity() 又清掉了什么?
android
2501_9159090610 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
Lvan的前端笔记10 小时前
AndroidX 完全入门指南
android·androidx
帅次11 小时前
Android 应用高级面试:Window 近1年高频追问 18 题
android·面试·职场和发展
总捣什么乱111 小时前
MySQL MySQL是怎么保证主备一致的?
android·mysql·adb
zzm62812 小时前
精读 HinDroid:基于异构信息网络的安卓恶意软件检测
android