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

发布于 2024-09-04 01:20:33 字数 68 浏览 5 评论 0原文

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

I am working on a multimedia application. I am capturing one image through the camera and want to send that image with a text to some other number. But I am not getting how to send the image via the MMS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失去的东西太少 2024-09-11 01:20:33

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-APN 的 url、MMSProxy:来自 MMS-APN 的代理、端口:来自 MMS-APN 的端口

请注意,某些类来自内部包。需要从 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;

}

MMS is just a htttp-post request. You should perform the request using extra network feature :

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

If you get result with Phone.APN_REQUEST_STARTED value, you have to wait for proper state. Register BroadCastReciver and wait until Phone.APN_ALREADY_ACTIVE appears:

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

If connection background is ready, build content and perform request. If you want to do that using android's internal code, please use this:

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: url from MMS-APNs, MMSProxy: proxy from MMS-APNs, port: port from MMS-APNs

Note that some classes are from internal packages. Download from android git is required.

The request should be done with url from user's apn-space...code..:

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;

}
和影子一齐双人舞 2024-09-11 01:20:33

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

This seems to be answered in the post: Sending MMS with Android

Key lines of code being:

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"); 
无边思念无边月 2024-09-11 01:20:33

如果您必须使用意图发送包含任何图像的彩信,请使用此代码。

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

让我知道这是否对您有帮助。

If you have to send MMS with any Image using Intent then use this code.

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

If you have to send MMS with Audio or Video file using Intent then use this.

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

let me know if this help you.

俏︾媚 2024-09-11 01:20:33

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

The answer with the APN helper will not work after android 4.0. To get mms apn settings on Android 4.0 and above view this answer: View mms apn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文