有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java发送带有位图的彩信源代码

我想通过点击片段中的按钮发送Bitmap和一些文本,我想让用户在他的消息应用程序中选择联系人/号码

我怎么能做到

编辑:

尝试某个操作后,我在消息应用程序(emulator上的Messenger)中出错:

Messenger failed to load attachment.

以下是我使用的代码:

String path = Environment.getExternalStorageDirectory().getPath() + "/Improov/LatestShare.png";
File file = new File(path);
FileOutputStream out = null;
try {
    out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
         e.printStackTrace();
    }
}
Uri photoURI = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".com.example.mous.improov_flash", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, photoURI);
intent.setType("image/jpeg");
getActivity().startActivity(intent);

共 (1) 个答案

  1. # 1 楼答案

    如果你想发送彩信

    首先,您应该将位图存储在sd卡中,然后使用以下意图发送彩信:

    获取存储位图的Uri

    Uri uri = [uri for your stored bitmap];
    
    Intent intent = new Intent(Intent.ACTION_SEND);
    inetnt.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra("sms_body", [Text to be send]);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.setType("image/jpeg");
    getActivity.startActivity(intent);
    

    欲知更多详情,请阅读此official doc