2

I'm trying to insert a MMS into the sent database but alas I haven't been able to view it in the native android application.

my insertion code:

ContentValues values = new ContentValues();
values.put("thread_id", thread_id);
values.put("date", time);
values.put("read", true); //read status
values.put("sub", text); //mms subject
values.put("msg_box", 2); //message box. in this case outbox

Uri mmsUri = context.getContentResolver().
insert(Uri.parse("content://mms"), values);
Log.v("MMSProjectActivity", "Message saved at: " + mmsUri);

ContentValues mmsPartValue = new ContentValues();
mmsPartValue.put("ct", "image/jpeg"); //mime; for example image/jpeg
Uri picUri = picUris.get(0);
String [] fileNameSplit = picUri.toString().split("/");
String fileName = fileNameSplit[fileNameSplit.length-1] + ".jpg";

String messageId = mmsUri.getLastPathSegment().trim(); //id of MMS at content://mms
Uri partUri = Uri.parse("content://mms/" + messageId + "/part");

Uri mmsPartUri = context.getContentResolver().insert(partUri, mmsPartValue);
OutputStream os;
InputStream is;
try
{
    os = context.getContentResolver().openOutputStream(mmsPartUri);
is = context.getContentResolver().openInputStream(picUris.get(0));
byte[] buffer = new byte[256];
for (int len = 0; (len = is.read(buffer)) != -1; ) {
    os.write(buffer, 0, len);
}
} catch (FileNotFoundException e)
{
Log.v("MMSProjectActivity", "MMS not saved FileNotFoundException");
e.printStackTrace();
} catch (IOException e)
{
Log.v("MMSProjectActivity", "MMS not saved IOException");
    e.printStackTrace();
}

Log.v("MMSProjectActivity", "MMS part value saved at: " + mmsPartUri);

anybody have any idea what am I doing wrong?

zwebie
  • 2,349
  • 1
  • 27
  • 43
  • some error logs would be helpful :) – Tancho Jul 26 '12 at 16:37
  • no error logs to show... – zwebie Jul 26 '12 at 16:40
  • Interesting.can you verify that the database has all the rows inserted. I mean can you verify that your record has been saved? Did you verify after insert ? Take a look at : http://gitorious.org/0xdroid/packages_providers_telephonyprovider/blobs/7236c3ad7ff01dd5ece14a2cabbf5ce3a570d793/src/com/android/providers/telephony/MmsProvider.java this is how the MMS app queries the MMS content.. are you filling all the fields? are you saving the pdu fields and such? take a look also at Google's Message sender : http://hi-android.info/src/com/android/mms/transaction/MmsMessageSender.java.html – Tancho Jul 26 '12 at 22:58

1 Answers1

0

I think what you need is in this class of the source code . Generally take a look at how they do it at google.. specifically take a look at this method

    private static Uri createDraftMmsMessage(PduPersister persister, SendReq sendReq,
        SlideshowModel slideshow) {
    try {
        PduBody pb = slideshow.toPduBody();
        sendReq.setBody(pb);
        Uri res = persister.persist(sendReq, Mms.Draft.CONTENT_URI);
        slideshow.sync(pb);
        return res;
    } catch (MmsException e) {
        return null;
    }
}

And after creating the Draft (step one) then you update the draft to sent. by calling the other method

 private static void updateDraftMmsMessage(Uri uri, PduPersister persister,
        SlideshowModel slideshow, SendReq sendReq) {
    if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
        LogTag.debug("updateDraftMmsMessage uri=%s", uri);
    }
    if (uri == null) {
        Log.e(TAG, "updateDraftMmsMessage null uri");
        return;
    }
    persister.updateHeaders(uri, sendReq);
    final PduBody pb = slideshow.toPduBody();

    try {
        persister.updateParts(uri, pb);
    } catch (MmsException e) {
        Log.e(TAG, "updateDraftMmsMessage: cannot update message " + uri);
    }

    slideshow.sync(pb);
}

Now I know you cannot run this code from your app since you're not building in the source, or even if you are it may be a challenge to do so (even though I think that if you do build in the source if you code correctly the google code should handle the save stuff)

in any case you should be able to save mms message in the provider by following what they do in this class.

cheers...

and post your progress...

Tancho
  • 1,581
  • 3
  • 22
  • 40
  • 1
    So I was more or less able to get it working I had to add a few more fields to that database and after following your answer I needed to extract a message-id from the confirmation and move persist the message again into the sent box...for some reason though I wasn't able to see address of the contact when querying it in the conversation table, I think this has to do with some field which I din't update correctly at some point...Thanks for the help! – zwebie Jul 29 '12 at 10:49
  • zwebie, do you edit the respective changes in you question or you can post your answer.. – kamal_tech_view Apr 02 '13 at 07:02