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

时间:2010-06-04 09:23:41

标签: android messaging mms

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

4 个答案:

答案 0 :(得分:39)

彩信只是一个htttp-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-APN的网址,MMSProxy:来自MMS-APN的代理,端口:来自MMS-APN的端口

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

请求应该来自用户的apn-space ...代码..:

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;

}

答案 1 :(得分:9)

这似乎在帖子中得到了解答: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"); 

答案 2 :(得分:9)

如果您必须使用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);;

OR

如果您必须使用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);

请告诉我这是否有帮助。

答案 3 :(得分:1)

在Android 4.0之后,APN助手的答案将无效。要在Android 4.0及更高版本上获取mms apn设置,请查看以下答案:View mms apn