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

结果:

相关推荐
geovindu11 小时前
sql:Search-Optimized SQL Queries using mysql 9.0
android·数据库·sql·mysql·数据库开发·数据库架构
知之则吱吱12 小时前
Android 开发者验证机制迎来新变化:核心要求、实施时间与开发者应对指南
android·google
墨天梦14 小时前
D09_Retrofit接口与数据转换
android·kotlin·retrofit
电商API_1800790524715 小时前
闲鱼商品列表API接口商品详情API接口请求示例(含一份真实响应数据示例)
android·数据挖掘·网络爬虫·跨境电商·反向海淘
我命由我1234515 小时前
Android 广播 - 组件通信
android·java·开发语言·android studio·android jetpack·android-studio·android runtime
阿俊-全栈开发15 小时前
LikeShop 单商户标准版商品管理二开:分类、SKU 规格与库存联动实现
android·likeshop·likeshop开源商城
用户21816970493018 小时前
Kotlin 语法二
android
妖星18 小时前
android 源码分析surfaceFinger是如何通过设置属性启动bootanim
android
JoyMonitor18 小时前
APK mmap 内存增长定位:pagemap 与 DEX 结构联合定位方法
android