我想将一些彩信添加到我的设备数据库中。
我有以下代码,但它根本不起作用。没有条目被添加到本机应用程序中......
public static Uri insert(Context context, String[] to, String subject, Uri messageUri)
{
try
{
Uri destUri = Uri.parse("content://mms/sent");
// Get thread id
Set<String> recipients = new HashSet<String>();
recipients.addAll(Arrays.asList(to));
long thread_id = getOrCreateThreadId(context, recipients);
Log.e(">>>>>>>", "Thread ID is " + thread_id);
// Create a dummy sms
ContentValues dummyValues = new ContentValues();
dummyValues.put("thread_id", thread_id);
dummyValues.put("body", "Dummy SMS body.");
Uri dummySms = context.getContentResolver().insert(Uri.parse("content://sms/sent"), dummyValues);
// Create a new message entry
ContentValues mmsValues = new ContentValues();
mmsValues.put("thread_id", thread_id);
mmsValues.put("date", System.currentTimeMillis()/1000);
mmsValues.put("ct_t", "application/vnd.wap.multipart.related");
mmsValues.put("read", "1");
mmsValues.put("sub", subject);
// Create part
long dummyId = System.currentTimeMillis();
createPart(context, dummyId, imageBytes);
// Insert message
Uri res = context.getContentResolver().insert(destUri, mmsValues);
String messageId = res.getLastPathSegment().trim();
Log.e(">>>>>>>", "Message saved as " + res);
// Update part
ContentValues updateValues = new ContentValues();
updateValues.put("mid", messageId);
Uri updateUri = Uri.parse("content://mms/" + dummyId + "/part");
int mmsPartRows = context.getContentResolver().update(updateUri, updateValues, null, null);
Log.e(">>>>>>>", "Part rows " + mmsPartRows);
// Create addresses
for (String addr : to)
{
ContentValues addrValues = new ContentValues();
addrValues.put("address", addr);
addrValues.put("charset", "106");
addrValues.put("type", 151); // TO
Uri addrUri = Uri.parse("content://mms/"+ messageId +"/addr");
Uri mmsAddrUri = context.getContentResolver().insert(addrUri, addrValues);
Log.e(">>>>>>>", "Addr uri is " + mmsAddrUri.toString());
}
res = Uri.parse(destUri + "/" + messageId);
// Delete dummy sms
context.getContentResolver().delete(dummySms, null, null);
return res;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static Uri createPart(Context context, long id, byte[] imageBytes) throws Exception
{
ContentValues mmsPartValue = new ContentValues();
mmsPartValue.put("ct", "image/png");
Uri partUri = Uri.parse("content://mms/" + id + "/part");
Uri res = context.getContentResolver().insert(partUri, mmsPartValue);
Log.e(">>>>>>>", "Part uri is " + res.toString());
// Add data to part
OutputStream os = context.getContentResolver().openOutputStream(res);
ByteArrayInputStream is = new ByteArrayInputStream(imageBytes);
byte[] buffer = new byte[256];
for (int len=0; (len=is.read(buffer)) != -1;)
{
os.write(buffer, 0, len);
}
os.close();
is.close();
return res;
}
private static long getOrCreateThreadId(Context context, String[] numbers)
{
HashSet<String> recipients = new HashSet<String>();
recipients.addAll(Arrays.asList(numbers));
return Telephony.Threads.getOrCreateThreadId(context, recipients);
}
context
是我的观点上下文to
是一个包含地址的字符串数组(例如 new String[] {"0612345678", "0623456789"})subject
是我的彩信主题,例如“通过 MyApp 发送”messageUri
是一个指向我想在我的 SD 卡上发送的图像的 Uri。
我做错了吗?