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

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

MMS只是一个htttp-post请求。 您应该使用额外的networkingfunction执行请求:

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

如果连接背景已准备就绪,则构build内容并执行请求。 如果你想用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-APN的URL,MMSProxy:来自MMS-APN的代理,来自MMS-APN的端口:端口

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

请求应该从用户的apn空间的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; } 

这似乎是在回复: 发送彩信与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"); 

如果您必须使用意图发送任何图像彩信,然后使用此代码。

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

要么

如果您必须使用意图发送audio或video文件的彩信,然后使用此。

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

让我知道这是否有助于你。

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