2010-06-04 113 views
43

我正在研究多媒體應用程序。我通過相機拍攝了一張圖像,並希望將該圖像以文本發送給其他號碼。但我不知道如何通過MMS發送圖像。如何在Android中通過彩信發送圖像?

+0

我正在做類似的事情! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – toobsco42 2013-01-22 06:33:48

回答

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

要記住的是圖像應該在外部存儲或作爲內容提供。通常內部應用程序數據不能被其他應用程序訪問。所以你必須暫時寫圖像到外部存儲器,然後將路徑傳遞到'Uri.parse' – Shubhank 2013-05-08 13:18:28

+0

這不再起作用了,因爲在手機中選擇消息活動會使消息僅包含圖片,而不包含文本 – peresisUser 2018-01-31 13:31:46

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

如果你得到結果和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,端口代理:從MMS-的APN,MMSProxy URL端口從MMS-APN的

注有些類來自內部包。從android git下載是必需的。

的請求應該從用戶的APN空間網址...代碼來完成..:

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; 

} 
+5

有人可以解釋如何正確地將所有引用的代碼導入到Eclipse中(例如SendReq類等)?我有git和repo,並且擁有android源代碼,並且在Eclipse中設置了MMS項目(我在SDK中找到了它/ packages/apps/MMS)。但是,它引用了Android系統的很多其他部分,我不知道如何在Eclipse中將所有這些引用放到我的項目中,而無需手動將文件複製到我的項目的src目錄和適當的子目錄中,這是非常困難的。 – 2011-03-16 02:27:27

+1

很多這些類似乎來自非公開的Google API(cfr:Mms應用程序)。我不認爲它們可以用於開箱即用。 – ddewaele 2011-03-27 10:19:17

+0

這段代碼是否調用內置的MMS應用程序? – Vijaya 2011-06-29 11:06:10

9

如果你有發送彩信使用意圖,然後使用該代碼的任何圖片

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

這將調用本地消息傳送應用程序。但是有沒有一種方法可以在你自己的應用程序中發送彩信,並以類似於如何實現** SMS **消息的方式註冊收聽帶有「BroadcastReceiver」的傳入** MMS **消息。 – toobsco42 2013-01-22 05:28:46

+0

我發現這可以正常使用圖像,但不是音頻/視頻(您的第二個代碼示例)。 – dadude999 2015-05-22 04:17:26

1

在Android 4.0之後,APN助手的答案將不起作用。要獲得Android 4.0及以上版本的mms apn設置,請參閱此答案:View mms apn