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

结果:

相关推荐
alexhilton5 小时前
MVI模式的完整历史、误解和现代Android范式
android·kotlin·android jetpack
心中有国也有家11 小时前
AtomGit Flutter 鸿蒙客户端:Canvas 绘制进阶-路径、渐变与混合模式
android·javascript·flutter·华为·harmonyos
hunterandroid12 小时前
WorkManager 可靠性实战:唯一任务、重试与幂等设计
android·前端
程序员老刘12 小时前
Android 17的10个AI功能,国内用户真正能用的有几个?
android·flutter·ai编程
花开彼岸天~13 小时前
鸿蒙应用开发实战【94】— 实战多卡号场景管理最佳实践
android·华为·harmonyos·鸿蒙系统
用户416596736935513 小时前
视频源文件音频轨异常分析记录
android
我命由我1234514 小时前
集成 mupdf-android 的示例做为模块使用报错,程序包android.support.v4.view不存在
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
雨白16 小时前
嵌套 ScrollView 滑动冲突实战:实现内部优先滚动
android
阿pin16 小时前
Android随笔-IntentService详解(了解)
android·intentservice
海天鹰16 小时前
content://com.android.externalstorage.documents/document/primary%3A
android