gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:50:31 29 4
gpt4 key购买 nike

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

最佳答案

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-APNs 的 url,MMSProxy:来自 MMS-APNs 的代理,port:来自 MMS-APNs 的端口

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

}

关于android - 如何在 Android 中通过彩信发送图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2972845/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com