如何在Android中通过彩信发送图像?

San*_*jay 45 messaging android mms

我正在研究多媒体应用程序.我正在通过相机捕获一个图像,并希望将带有文本的图像发送到其他一些数字.但我没有得到如何通过彩信发送图像.

Dam*_*ski 39

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

final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = connMgr.startUsingNetworkFeature( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS);
Run Code Online (Sandbox Code Playgroud)

如果您使用Phone.APN_REQUEST_STARTED值获得结果,则必须等待正确的状态.注册BroadCastReciver并等到Phone.APN_ALREADY_ACTIVE出现:

final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(reciver, filter);
Run Code Online (Sandbox Code Playgroud)

如果连接后台准备就绪,则构建内容并执行请求.如果你想使用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);
Run Code Online (Sandbox Code Playgroud)

MMSCenterUrl:来自MMS-APN的URL,MMSProxy:来自MMS-APN的代理,端口:来自MMS-APN的端口

请注意,某些类来自内部包.从android git下载是必需的.

请求应该使用来自用户的apn-space ...代码的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;

}
Run Code Online (Sandbox Code Playgroud)

  • 有人可以解释如何正确地将所有引用的代码都引入Eclipse(例如SendReq类等)吗?我有git和repo并拥有android源码并且已经在Eclipse中设置了MMS项目(我在/ packages/apps/MMS中的SDK中找到了它).但是,它引用了Android系统的许多其他部分,我不知道如何在Eclipse中将所有这些引用都放入我的项目中,而无需手动将文件复制到我的项目的src目录和正确的子目录中,这是压倒性的. (5认同)

aio*_*obe 9

这似乎在帖子中回答:使用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"); 
Run Code Online (Sandbox Code Playgroud)

  • 要记住的是图像应该在外部存储器上或作为内容提供.通常,其他应用无法访问内部应用数据.所以你必须暂时将图像写入外部存储器,然后将路径传递给`Uri.parse` (2认同)

swi*_*Boy 9

如果您必须使用Intent使用任何Image发送MMS,请使用此代码.

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);;
Run Code Online (Sandbox Code Playgroud)

要么

如果您必须使用Intent 发送带有音频或视频文件的MMS,请使用此功能.

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);
Run Code Online (Sandbox Code Playgroud)

如果这有助于你,请告诉我.

  • 这将调用本机消息传递应用程序.但是有没有办法在你自己的应用程序中发送彩信,并使用"BroadcastReceiver"注册收听传入的**MMS**消息,其方式与**SMS**消息的实现类似. (3认同)