【问题标题】:How to send image via MMS in Android?如何在 Android 中通过彩信发送图像?
【发布时间】:2015-05-13 14:34:39
【问题描述】:

我正在开发一个多媒体应用程序。我正在通过相机捕获一张图像,并希望将该图像与文本一起发送到其他号码。但我不知道如何通过彩信发送图像。

【问题讨论】:

标签: android messaging mms


【解决方案1】:

MMS 只是一个 http-post 请求。您应该使用额外的网络功能来执行请求:

final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = connMgr.startUsingNetworkFeature( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS);

如果你得到带有 Phone.APN_REQUEST_STARTED 值的结果,你必须等待正确的状态。注册BroadCastReciver,等待Phone.APN_ALREADY_ACTIVE出现:

final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(reciver, filter);

如果连接后台准备好,构建内容并执行请求。如果您想使用 android 的内部代码执行此操作,请使用以下代码:

final SendReq sendRequest = new SendReq();
    final EncodedStringValue[] sub = EncodedStringValue.extract(subject);
    if (sub != null && sub.length > 0) {
        sendRequest.setSubject(sub[0]);
    }
    final EncodedStringValue[] phoneNumbers = EncodedStringValue
            .extract(recipient);
    if (phoneNumbers != null && phoneNumbers.length > 0) {
        sendRequest.addTo(phoneNumbers[0]);
    }

    final PduBody pduBody = new PduBody();

    if (parts != null) {
        for (MMSPart part : parts) {
            final PduPart partPdu = new PduPart();
            partPdu.setName(part.Name.getBytes());
            partPdu.setContentType(part.MimeType.getBytes());
            partPdu.setData(part.Data);
            pduBody.addPart(partPdu);
        }
    }

    sendRequest.setBody(pduBody);

    final PduComposer composer = new PduComposer(this.context, sendRequest);
    final byte[] bytesToSend = composer.make();

    HttpUtils.httpConnection(context, 4444L, MMSCenterUrl,
            bytesToSendFromPDU, HttpUtils.HTTP_POST_METHOD, !TextUtils
                    .isEmpty(MMSProxy), MMSProxy, port);

MMSCenterUrl:来自 MMS-APNs 的 url,MMSProxy:来自 MMS-APNs 的代理,port:来自 MMS-APNs 的端口

请注意,有些类来自内部包。需要从 android git 下载。

请求应该使用来自用户 apn-space...code.. 的 url 完成:

public class APNHelper {

public class APN {
    public String MMSCenterUrl = "";
    public String MMSPort = "";
    public String MMSProxy = ""; 
}

public APNHelper(final Context context) {
    this.context = context;
}   

public List<APN> getMMSApns() {     
    final Cursor apnCursor = this.context.getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), null, null, null, null);
if ( apnCursor == null ) {
        return Collections.EMPTY_LIST;
    } else {
        final List<APN> results = new ArrayList<APN>(); 
            if ( apnCursor.moveToFirst() ) {
        do {
            final String type = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.TYPE));
            if ( !TextUtils.isEmpty(type) && ( type.equalsIgnoreCase(Phone.APN_TYPE_ALL) || type.equalsIgnoreCase(Phone.APN_TYPE_MMS) ) ) {
                final String mmsc = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSC));
                final String mmsProxy = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPROXY));
                final String port = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPORT));                  
                final APN apn = new APN();
                apn.MMSCenterUrl = mmsc;
                apn.MMSProxy = mmsProxy;
                apn.MMSPort = port;
                results.add(apn);
            }
        } while ( apnCursor.moveToNext() ); 
             }              
        apnCursor.close();
        return results;
    }
}

private Context context;

}

【讨论】:

  • 有人能解释一下如何正确地将所有引用的代码放入 Eclipse(例如 SendReq 类等)吗?我有 git 和 repo 并有 android 源代码,并在 Eclipse 中设置了 MMS 项目本身(我在 /packages/apps/MMS 的 SDK 中找到了它)。但是,它引用了 Android 系统的许多其他部分,如果不手动将文件复制到我的项目的 src 目录和适当的子目录中,我不知道如何将所有这些引用放入我的项目中,这是压倒性的。跨度>
  • 很多这些类似乎来自非公开的 google API(cfr:Mms 应用程序)。我认为它们不能开箱即用。
  • 完全有效!非常感谢! (另外,由于某种原因,我不得不在 HttpUtils 中将 AndroidHttpClient 更改为 DefaultHttpClient)
  • 请提供更清晰的实现,我想在我的项目中以编程方式发送彩信。但无法使用您显示的代码..
【解决方案2】:

这个帖子好像回答了:Sending MMS with Android

关键代码行是:

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.putExtra("sms_body", "some text"); 
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
sendIntent.setType("image/png"); 

【讨论】:

  • 要记住的是图像应该在外部存储上或作为内容提供。通常内部应用程序数据不能被其他应用程序访问。所以你必须暂时将图像写入外部存储,然后将路径传递给Uri.parse
  • 这不再起作用,因为在手机中选择消息活动会使消息只包含图片,不包含文本
  • @peresisUser 对目前的工作有什么想法或代码 sn-ps?
【解决方案3】:

如果您必须使用 Intent 发送带有任何图像的彩信,请使用此代码。

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("sms_body", "some text"); 
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/image_4.png"));
sendIntent.setType("image/png");
startActivity(sendIntent);;

如果您必须使用 Intent 发送带有音频或视频的彩信文件,请使用它。

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("address", "1213123123");
sendIntent.putExtra("sms_body", "if you are sending text");   
final File file1 = new File(mFileName);
if(file1.exists()){
  System.out.println("file is exist");
}
Uri uri = Uri.fromFile(file1);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("video/*");
startActivity(sendIntent);

如果这对你有帮助,请告诉我。

【讨论】:

  • 这会调用本机消息传递应用程序。但是有没有办法在您自己的应用程序中发送 MMS 并使用 BroadcastReceiver 注册侦听传入的 MMS 消息,其方式类似于 SMS 消息的实现方式.
  • 我发现这适用于图像,但不适用于音频/视频(您的第二个代码示例)。
【解决方案4】:

android 4.0 之后,使用 APN 助手的答案将不起作用。要在 Android 4.0 及更高版本上获取 mms apn 设置,请查看此答案:View mms apn

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多